Fix some PMM corner-cases.
[seabios.git] / src / pmm.c
1 // Post memory manager (PMM) calls
2 //
3 // Copyright (C) 2009  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6
7 #include "util.h" // checksum
8 #include "config.h" // BUILD_BIOS_ADDR
9 #include "memmap.h" // e820_list
10 #include "farptr.h" // GET_FARVAR
11 #include "biosvar.h" // EBDA_SEGMENT_MINIMUM
12
13
14 /****************************************************************
15  * malloc
16  ****************************************************************/
17
18 #if MODE16
19 // The 16bit pmm entry points runs in "big real" mode, and can
20 // therefore read/write to the 32bit malloc variables.
21 #define GET_PMMVAR(var) GET_FARVAR(0, (var))
22 #define SET_PMMVAR(var, val) SET_FARVAR(0, (var), (val))
23 #else
24 #define GET_PMMVAR(var) (var)
25 #define SET_PMMVAR(var, val) do { (var) = (val); } while (0)
26 #endif
27
28 // Zone definitions
29 struct zone_s {
30     u32 top, bottom, cur;
31 };
32
33 struct zone_s ZoneHigh VAR32VISIBLE, ZoneFSeg VAR32VISIBLE;
34 struct zone_s ZoneTmpLow VAR32VISIBLE, ZoneTmpHigh VAR32VISIBLE;
35
36 struct zone_s *Zones[] VAR32VISIBLE = {
37     &ZoneTmpLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
38 };
39
40 // Obtain memory from a given zone.
41 static void *
42 zone_malloc(struct zone_s *zone, u32 size, u32 align)
43 {
44     u32 newpos = (GET_PMMVAR(zone->cur) - size) / align * align;
45     if ((s32)(newpos - GET_PMMVAR(zone->bottom)) < 0)
46         // No space
47         return NULL;
48     SET_PMMVAR(zone->cur, newpos);
49     return (void*)newpos;
50 }
51
52 // Return memory to a zone (if it was the last to be allocated).
53 static void
54 zone_free(struct zone_s *zone, void *data, u32 olddata)
55 {
56     if (! data || GET_PMMVAR(zone->cur) != (u32)data)
57         return;
58     SET_PMMVAR(zone->cur, olddata);
59 }
60
61 // Find the zone that contains the given data block.
62 static struct zone_s *
63 zone_find(void *data)
64 {
65     int i;
66     for (i=0; i<ARRAY_SIZE(Zones); i++) {
67         struct zone_s *zone = GET_PMMVAR(Zones[i]);
68         if ((u32)data >= GET_PMMVAR(zone->cur)
69             && (u32)data < GET_PMMVAR(zone->top))
70             return zone;
71     }
72     return NULL;
73 }
74
75 // Report the status of all the zones.
76 static void
77 dumpZones()
78 {
79     int i;
80     for (i=0; i<ARRAY_SIZE(Zones); i++) {
81         struct zone_s *zone = Zones[i];
82         u32 used = zone->top - zone->cur;
83         u32 avail = zone->top - zone->bottom;
84         u32 pct = avail ? ((100 * used) / avail) : 0;
85         dprintf(2, "zone %d: %08x-%08x used=%d (%d%%)\n"
86                 , i, zone->bottom, zone->top, used, pct);
87     }
88 }
89
90 // Allocate memory at the top of 32bit ram.
91 void *
92 malloc_high(u32 size)
93 {
94     return zone_malloc(&ZoneHigh, size, MALLOC_MIN_ALIGN);
95 }
96
97 // Allocate memory in the 0xf0000-0x100000 area of ram.
98 void *
99 malloc_fseg(u32 size)
100 {
101     return zone_malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
102 }
103
104 void
105 malloc_setup()
106 {
107     ASSERT32();
108     dprintf(3, "malloc setup\n");
109
110     // Memory in 0xf0000 area.
111     memset(BiosTableSpace, 0, CONFIG_MAX_BIOSTABLE);
112     ZoneFSeg.bottom = (u32)BiosTableSpace;
113     ZoneFSeg.top = ZoneFSeg.cur = ZoneFSeg.bottom + CONFIG_MAX_BIOSTABLE;
114
115     // Memory under 1Meg.
116     ZoneTmpLow.bottom = BUILD_STACK_ADDR;
117     ZoneTmpLow.top = ZoneTmpLow.cur = (u32)MAKE_FLATPTR(EBDA_SEGMENT_MINIMUM, 0);
118
119     // Find memory at the top of ram.
120     u32 top = 0, bottom = 0;
121     int i;
122     for (i=e820_count-1; i>=0; i--) {
123         struct e820entry *e = &e820_list[i];
124         u64 end = e->start + e->size;
125         if (e->type != E820_RAM || end > 0xffffffff
126             || e->size < CONFIG_MAX_HIGHTABLE + MALLOC_MIN_ALIGN)
127             continue;
128         top = end;
129         bottom = e->start;
130         break;
131     }
132     if (top < 1024*1024 + CONFIG_MAX_HIGHTABLE) {
133         // No memory above 1Meg
134         memset(&ZoneHigh, 0, sizeof(ZoneHigh));
135         memset(&ZoneTmpHigh, 0, sizeof(ZoneHigh));
136         return;
137     }
138
139     // Memory at top of ram.
140     ZoneHigh.bottom = ALIGN(top - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
141     ZoneHigh.top = ZoneHigh.cur = ZoneHigh.bottom + CONFIG_MAX_HIGHTABLE;
142     add_e820(ZoneHigh.bottom, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
143
144     // Memory above 1Meg
145     ZoneTmpHigh.bottom = ALIGN(bottom, MALLOC_MIN_ALIGN);
146     ZoneTmpHigh.top = ZoneTmpHigh.cur = ZoneHigh.bottom;
147 }
148
149 void
150 malloc_finalize()
151 {
152     dprintf(3, "malloc finalize\n");
153
154     dumpZones();
155
156     // Give back unused high ram.
157     u32 giveback = (ZoneHigh.cur - ZoneHigh.bottom) / 4096 * 4096;
158     add_e820(ZoneHigh.bottom, giveback, E820_RAM);
159     dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
160
161     // Clear low-memory allocations.
162     memset((void*)ZoneTmpLow.bottom, 0, ZoneTmpLow.top - ZoneTmpLow.bottom);
163 }
164
165
166 /****************************************************************
167  * pmm allocation
168  ****************************************************************/
169
170 // Information on PMM tracked allocations
171 struct pmmalloc_s {
172     void *data;
173     u32 olddata;
174     u32 handle;
175     u32 oldallocdata;
176     struct pmmalloc_s *next;
177 };
178
179 struct pmmalloc_s *PMMAllocs VAR32VISIBLE;
180
181 // Memory zone that pmm allocation tracking info is stored in
182 #define ZONEALLOC (&ZoneTmpHigh)
183
184 // Allocate memory from the given zone and track it as a PMM allocation
185 static void *
186 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
187 {
188     u32 oldallocdata = GET_PMMVAR(ZONEALLOC->cur);
189     struct pmmalloc_s *info = zone_malloc(ZONEALLOC, sizeof(*info)
190                                           , MALLOC_MIN_ALIGN);
191     if (!info)
192         return NULL;
193     u32 olddata = GET_PMMVAR(zone->cur);
194     void *data = zone_malloc(zone, size, align);
195     if (! data) {
196         zone_free(ZONEALLOC, info, oldallocdata);
197         return NULL;
198     }
199     dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
200             " ret=%p (info=%p)\n"
201             , zone, handle, size, align
202             , data, info);
203     SET_PMMVAR(info->data, data);
204     SET_PMMVAR(info->olddata, olddata);
205     SET_PMMVAR(info->handle, handle);
206     SET_PMMVAR(info->oldallocdata, oldallocdata);
207     SET_PMMVAR(info->next, GET_PMMVAR(PMMAllocs));
208     SET_PMMVAR(PMMAllocs, info);
209     return data;
210 }
211
212 // Free a raw data block (either from a zone or from pmm alloc list).
213 static void
214 pmm_free_data(struct zone_s *zone, void *data, u32 olddata)
215 {
216     if (GET_PMMVAR(zone->cur) == (u32)data) {
217         zone_free(zone, data, olddata);
218         return;
219     }
220     struct pmmalloc_s *info;
221     for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
222         if (GET_PMMVAR(info->olddata) == (u32)data) {
223             SET_PMMVAR(info->olddata, olddata);
224             return;
225         } else if (GET_PMMVAR(info->oldallocdata) == (u32)data) {
226             SET_PMMVAR(info->oldallocdata, olddata);
227             return;
228         }
229 }
230
231 // Free a data block allocated with pmm_malloc
232 static int
233 pmm_free(void *data)
234 {
235     struct zone_s *zone = zone_find(GET_PMMVAR(data));
236     if (!zone)
237         return -1;
238     struct pmmalloc_s **pinfo = &PMMAllocs;
239     for (;;) {
240         struct pmmalloc_s *info = GET_PMMVAR(*pinfo);
241         if (!info)
242             return -1;
243         if (GET_PMMVAR(info->data) == data) {
244             SET_PMMVAR(*pinfo, GET_PMMVAR(info->next));
245             u32 oldallocdata = GET_PMMVAR(info->oldallocdata);
246             u32 olddata = GET_PMMVAR(info->olddata);
247             pmm_free_data(zone, data, olddata);
248             pmm_free_data(ZONEALLOC, info, oldallocdata);
249             dprintf(8, "pmm_free data=%p zone=%p olddata=%p oldallocdata=%p"
250                     " info=%p\n"
251                     , data, zone, (void*)olddata, (void*)oldallocdata
252                     , info);
253             return 0;
254         }
255         pinfo = &info->next;
256     }
257 }
258
259 // Find the amount of free space in a given zone.
260 static u32
261 pmm_getspace(struct zone_s *zone)
262 {
263     u32 space = GET_PMMVAR(zone->cur) - GET_PMMVAR(zone->bottom);
264     if (zone != ZONEALLOC)
265         return space;
266     u32 reserve = ALIGN(sizeof(struct pmmalloc_s), MALLOC_MIN_ALIGN);
267     if (space <= reserve)
268         return 0;
269     return space - reserve;
270 }
271
272 // Find the data block allocated with pmm_malloc with a given handle.
273 static void *
274 pmm_find(u32 handle)
275 {
276     struct pmmalloc_s *info;
277     for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
278         if (GET_PMMVAR(info->handle) == handle)
279             return GET_PMMVAR(info->data);
280     return NULL;
281 }
282
283
284 /****************************************************************
285  * pmm interface
286  ****************************************************************/
287
288 struct pmmheader {
289     u32 signature;
290     u8 version;
291     u8 length;
292     u8 checksum;
293     u16 entry_offset;
294     u16 entry_seg;
295     u8 reserved[5];
296 } PACKED;
297
298 extern struct pmmheader PMMHEADER;
299
300 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
301
302 #if CONFIG_PMM
303 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
304     .version = 0x01,
305     .length = sizeof(PMMHEADER),
306     .entry_seg = SEG_BIOS,
307 };
308 #endif
309
310 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
311
312 // PMM - allocate
313 static u32
314 handle_pmm00(u16 *args)
315 {
316     u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
317     u16 flags = args[5];
318     dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
319             , length, handle, flags);
320     if (!length) {
321         // Memory size request
322         switch (flags & 3) {
323         default:
324         case 0:
325             return 0;
326         case 1:
327             return pmm_getspace(&ZoneTmpLow);
328         case 2:
329             return pmm_getspace(&ZoneTmpHigh);
330         case 3: {
331             u32 spacelow = pmm_getspace(&ZoneTmpLow);
332             u32 spacehigh = pmm_getspace(&ZoneTmpHigh);
333             if (spacelow > spacehigh)
334                 return spacelow;
335             return spacehigh;
336         }
337         }
338     }
339     u32 size = length * 16;
340     if ((s32)size <= 0)
341         return 0;
342     u32 align = MALLOC_MIN_ALIGN;
343     if (flags & 4) {
344         align = 1<<__ffs(size);
345         if (align < MALLOC_MIN_ALIGN)
346             align = MALLOC_MIN_ALIGN;
347     }
348     switch (flags & 3) {
349     default:
350     case 0:
351         return 0;
352     case 1:
353         return (u32)pmm_malloc(&ZoneTmpLow, handle, size, align);
354     case 2:
355         return (u32)pmm_malloc(&ZoneTmpHigh, handle, size, align);
356     case 3: {
357         void *data = pmm_malloc(&ZoneTmpLow, handle, size, align);
358         if (data)
359             return (u32)data;
360         return (u32)pmm_malloc(&ZoneTmpHigh, handle, size, align);
361     }
362     }
363 }
364
365 // PMM - find
366 static u32
367 handle_pmm01(u16 *args)
368 {
369     u32 handle = *(u32*)&args[1];
370     dprintf(3, "pmm01: handle=%x\n", handle);
371     if (handle == 0xFFFFFFFF)
372         return 0;
373     return (u32)pmm_find(handle);
374 }
375
376 // PMM - deallocate
377 static u32
378 handle_pmm02(u16 *args)
379 {
380     u32 buffer = *(u32*)&args[1];
381     dprintf(3, "pmm02: buffer=%x\n", buffer);
382     int ret = pmm_free((void*)buffer);
383     if (ret)
384         // Error
385         return 1;
386     return 0;
387 }
388
389 static u32
390 handle_pmmXX(u16 *args)
391 {
392     return PMM_FUNCTION_NOT_SUPPORTED;
393 }
394
395 u32 VISIBLE16
396 handle_pmm(u16 *args)
397 {
398     if (! CONFIG_PMM)
399         return PMM_FUNCTION_NOT_SUPPORTED;
400
401     u16 arg1 = args[0];
402     dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
403
404     switch (arg1) {
405     case 0x00: return handle_pmm00(args);
406     case 0x01: return handle_pmm01(args);
407     case 0x02: return handle_pmm02(args);
408     default:   return handle_pmmXX(args);
409     }
410 }
411
412 // romlayout.S
413 extern void entry_pmm();
414
415 void
416 pmm_setup()
417 {
418     if (! CONFIG_PMM)
419         return;
420
421     dprintf(3, "init PMM\n");
422
423     PMMAllocs = NULL;
424
425     PMMHEADER.signature = PMM_SIGNATURE;
426     PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
427     PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
428 }
429
430 void
431 pmm_finalize()
432 {
433     if (! CONFIG_PMM)
434         return;
435
436     dprintf(3, "finalize PMM\n");
437
438     PMMHEADER.signature = 0;
439     PMMHEADER.entry_offset = 0;
440 }