Add support for permanent low memory allocations.
[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" // find_high_area
10 #include "farptr.h" // GET_FARVAR
11 #include "biosvar.h" // GET_BDA
12
13
14 #if MODE16
15 // The 16bit pmm entry points runs in "big real" mode, and can
16 // therefore read/write to the 32bit malloc variables.
17 #define GET_PMMVAR(var) GET_FARVAR(0, (var))
18 #define SET_PMMVAR(var, val) SET_FARVAR(0, (var), (val))
19 #else
20 #define GET_PMMVAR(var) (var)
21 #define SET_PMMVAR(var, val) do { (var) = (val); } while (0)
22 #endif
23
24 // Zone definitions
25 struct zone_s {
26     u32 top, bottom, cur;
27 };
28
29 struct zone_s ZoneLow VAR32VISIBLE, ZoneHigh VAR32VISIBLE;
30 struct zone_s ZoneFSeg VAR32VISIBLE;
31 struct zone_s ZoneTmpLow VAR32VISIBLE, ZoneTmpHigh VAR32VISIBLE;
32
33 struct zone_s *Zones[] VAR32VISIBLE = {
34     &ZoneTmpLow, &ZoneLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
35 };
36
37
38 /****************************************************************
39  * ebda movement
40  ****************************************************************/
41
42 // Move ebda
43 static int
44 relocate_ebda(u32 newebda, u32 oldebda, u8 ebda_size)
45 {
46     u32 lowram = GET_BDA(mem_size_kb) * 1024;
47     if (oldebda != lowram)
48         // EBDA isn't at end of ram - give up.
49         return -1;
50
51     // Do copy
52     if (MODE16)
53         memcpy_far(FLATPTR_TO_SEG(newebda)
54                    , (void*)FLATPTR_TO_OFFSET(newebda)
55                    , FLATPTR_TO_SEG(oldebda)
56                    , (void*)FLATPTR_TO_OFFSET(oldebda)
57                    , ebda_size * 1024);
58     else
59         memmove((void*)newebda, (void*)oldebda, ebda_size * 1024);
60
61     // Update indexes
62     dprintf(1, "ebda moved from %x to %x\n", oldebda, newebda);
63     SET_BDA(mem_size_kb, newebda / 1024);
64     SET_BDA(ebda_seg, FLATPTR_TO_SEG(newebda));
65     return 0;
66 }
67
68 // Support expanding the ZoneLow dynamically.
69 static int
70 zonelow_expand(u32 size, u32 align)
71 {
72     u16 ebda_seg = get_ebda_seg();
73     u32 ebda_pos = (u32)MAKE_FLATPTR(ebda_seg, 0);
74     u32 bottom = GET_PMMVAR(ZoneLow.bottom);
75     u32 cur = GET_PMMVAR(ZoneLow.cur);
76     u8 ebda_size = GET_EBDA2(ebda_seg, size);
77     u32 ebda_end = ebda_pos + ebda_size * 1024;
78     if (ebda_end != bottom)
79         // Something else is after ebda - can't use any existing space.
80         cur = ebda_end;
81     u32 newcur = ALIGN_DOWN(cur - size, align);
82     u32 newbottom = ALIGN_DOWN(newcur, 1024);
83     u32 newebda = ALIGN_DOWN(newbottom - ebda_size * 1024, 1024);
84     if (newebda < BUILD_EBDA_MINIMUM)
85         // Not enough space.
86         return -1;
87
88     // Move ebda
89     int ret = relocate_ebda(newebda, ebda_pos, ebda_size);
90     if (ret)
91         return ret;
92
93     // Update zone
94     SET_PMMVAR(ZoneLow.bottom, newbottom);
95     return 0;
96 }
97
98
99 /****************************************************************
100  * zone allocations
101  ****************************************************************/
102
103 // Obtain memory from a given zone.
104 void *
105 zone_malloc(struct zone_s *zone, u32 size, u32 align)
106 {
107     u32 oldpos = GET_PMMVAR(zone->cur);
108     u32 newpos = ALIGN_DOWN(oldpos - size, align);
109     if (newpos < GET_PMMVAR(zone->bottom) || newpos > oldpos)
110         // No space
111         return NULL;
112     SET_PMMVAR(zone->cur, newpos);
113     return (void*)newpos;
114 }
115
116 // Return memory to a zone (if it was the last to be allocated).
117 static void
118 zone_free(struct zone_s *zone, void *data, u32 olddata)
119 {
120     if (! data || GET_PMMVAR(zone->cur) != (u32)data)
121         return;
122     SET_PMMVAR(zone->cur, olddata);
123 }
124
125 // Find the zone that contains the given data block.
126 static struct zone_s *
127 zone_find(void *data)
128 {
129     int i;
130     for (i=0; i<ARRAY_SIZE(Zones); i++) {
131         struct zone_s *zone = GET_PMMVAR(Zones[i]);
132         if ((u32)data >= GET_PMMVAR(zone->cur)
133             && (u32)data < GET_PMMVAR(zone->top))
134             return zone;
135     }
136     return NULL;
137 }
138
139 // Report the status of all the zones.
140 static void
141 dumpZones()
142 {
143     int i;
144     for (i=0; i<ARRAY_SIZE(Zones); i++) {
145         struct zone_s *zone = Zones[i];
146         u32 used = zone->top - zone->cur;
147         u32 avail = zone->top - zone->bottom;
148         u32 pct = avail ? ((100 * used) / avail) : 0;
149         dprintf(2, "zone %d: %08x-%08x used=%d (%d%%)\n"
150                 , i, zone->bottom, zone->top, used, pct);
151     }
152 }
153
154 void
155 malloc_setup()
156 {
157     ASSERT32();
158     dprintf(3, "malloc setup\n");
159
160     // Memory in 0xf0000 area.
161     memset(BiosTableSpace, 0, CONFIG_MAX_BIOSTABLE);
162     ZoneFSeg.bottom = (u32)BiosTableSpace;
163     ZoneFSeg.top = ZoneFSeg.cur = ZoneFSeg.bottom + CONFIG_MAX_BIOSTABLE;
164
165     // Memory under 1Meg.
166     ZoneTmpLow.bottom = BUILD_STACK_ADDR;
167     ZoneTmpLow.top = ZoneTmpLow.cur = BUILD_EBDA_MINIMUM;
168
169     // Permanent memory under 1Meg.
170     ZoneLow.bottom = ZoneLow.top = ZoneLow.cur = BUILD_LOWRAM_END;
171
172     // Find memory at the top of ram.
173     struct e820entry *e = find_high_area(CONFIG_MAX_HIGHTABLE+MALLOC_MIN_ALIGN);
174     if (!e) {
175         // No memory above 1Meg
176         memset(&ZoneHigh, 0, sizeof(ZoneHigh));
177         memset(&ZoneTmpHigh, 0, sizeof(ZoneTmpHigh));
178         return;
179     }
180     u32 top = e->start + e->size, bottom = e->start;
181
182     // Memory at top of ram.
183     ZoneHigh.bottom = ALIGN(top - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
184     ZoneHigh.top = ZoneHigh.cur = ZoneHigh.bottom + CONFIG_MAX_HIGHTABLE;
185     add_e820(ZoneHigh.bottom, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
186
187     // Memory above 1Meg
188     ZoneTmpHigh.bottom = ALIGN(bottom, MALLOC_MIN_ALIGN);
189     ZoneTmpHigh.top = ZoneTmpHigh.cur = ZoneHigh.bottom;
190 }
191
192 void
193 malloc_finalize()
194 {
195     dprintf(3, "malloc finalize\n");
196
197     dumpZones();
198
199     // Reserve more low-mem if needed.
200     u32 endlow = GET_BDA(mem_size_kb)*1024;
201     add_e820(endlow, BUILD_LOWRAM_END-endlow, E820_RESERVED);
202
203     // Give back unused high ram.
204     u32 giveback = ALIGN_DOWN(ZoneHigh.cur - ZoneHigh.bottom, PAGE_SIZE);
205     add_e820(ZoneHigh.bottom, giveback, E820_RAM);
206     dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
207
208     // Clear low-memory allocations.
209     memset((void*)ZoneTmpLow.bottom, 0, ZoneTmpLow.top - ZoneTmpLow.bottom);
210 }
211
212 // Allocate memory from ZoneLow - growing the zone if needed.
213 void *
214 zone_malloc_low(u32 size, u32 align)
215 {
216     void *ret = zone_malloc(&ZoneLow, size, align);
217     if (ret)
218         return ret;
219     zonelow_expand(size, align);
220     return zone_malloc(&ZoneLow, size, align);
221 }
222
223
224 /****************************************************************
225  * pmm allocation
226  ****************************************************************/
227
228 // Information on PMM tracked allocations
229 struct pmmalloc_s {
230     void *data;
231     u32 olddata;
232     u32 handle;
233     u32 oldallocdata;
234     struct pmmalloc_s *next;
235 };
236
237 struct pmmalloc_s *PMMAllocs VAR32VISIBLE;
238
239 // Memory zone that pmm allocation tracking info is stored in
240 #define ZONEALLOC (&ZoneTmpHigh)
241
242 // Allocate memory from the given zone and track it as a PMM allocation
243 static void *
244 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
245 {
246     u32 oldallocdata = GET_PMMVAR(ZONEALLOC->cur);
247     struct pmmalloc_s *info = zone_malloc(ZONEALLOC, sizeof(*info)
248                                           , MALLOC_MIN_ALIGN);
249     if (!info)
250         return NULL;
251     u32 olddata = GET_PMMVAR(zone->cur);
252     void *data = zone_malloc(zone, size, align);
253     if (!data && zone == &ZoneLow) {
254         // Try to expand permanent low zone.
255         zonelow_expand(size, align);
256         olddata = GET_PMMVAR(zone->cur);
257         data = zone_malloc(zone, size, align);
258     }
259     if (! data) {
260         zone_free(ZONEALLOC, info, oldallocdata);
261         return NULL;
262     }
263     dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
264             " ret=%p (info=%p)\n"
265             , zone, handle, size, align
266             , data, info);
267     SET_PMMVAR(info->data, data);
268     SET_PMMVAR(info->olddata, olddata);
269     SET_PMMVAR(info->handle, handle);
270     SET_PMMVAR(info->oldallocdata, oldallocdata);
271     SET_PMMVAR(info->next, GET_PMMVAR(PMMAllocs));
272     SET_PMMVAR(PMMAllocs, info);
273     return data;
274 }
275
276 // Free a raw data block (either from a zone or from pmm alloc list).
277 static void
278 pmm_free_data(struct zone_s *zone, void *data, u32 olddata)
279 {
280     if (GET_PMMVAR(zone->cur) == (u32)data) {
281         zone_free(zone, data, olddata);
282         return;
283     }
284     struct pmmalloc_s *info;
285     for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
286         if (GET_PMMVAR(info->olddata) == (u32)data) {
287             SET_PMMVAR(info->olddata, olddata);
288             return;
289         } else if (GET_PMMVAR(info->oldallocdata) == (u32)data) {
290             SET_PMMVAR(info->oldallocdata, olddata);
291             return;
292         }
293 }
294
295 // Free a data block allocated with pmm_malloc
296 static int
297 pmm_free(void *data)
298 {
299     struct zone_s *zone = zone_find(GET_PMMVAR(data));
300     if (!zone)
301         return -1;
302     struct pmmalloc_s **pinfo = &PMMAllocs;
303     for (;;) {
304         struct pmmalloc_s *info = GET_PMMVAR(*pinfo);
305         if (!info)
306             return -1;
307         if (GET_PMMVAR(info->data) == data) {
308             SET_PMMVAR(*pinfo, GET_PMMVAR(info->next));
309             u32 oldallocdata = GET_PMMVAR(info->oldallocdata);
310             u32 olddata = GET_PMMVAR(info->olddata);
311             pmm_free_data(zone, data, olddata);
312             pmm_free_data(ZONEALLOC, info, oldallocdata);
313             dprintf(8, "pmm_free data=%p zone=%p olddata=%p oldallocdata=%p"
314                     " info=%p\n"
315                     , data, zone, (void*)olddata, (void*)oldallocdata
316                     , info);
317             return 0;
318         }
319         pinfo = &info->next;
320     }
321 }
322
323 // Find the amount of free space in a given zone.
324 static u32
325 pmm_getspace(struct zone_s *zone)
326 {
327     // XXX - doesn't account for ZoneLow being able to grow.
328     u32 space = GET_PMMVAR(zone->cur) - GET_PMMVAR(zone->bottom);
329     if (zone != ZONEALLOC)
330         return space;
331     u32 reserve = ALIGN(sizeof(struct pmmalloc_s), MALLOC_MIN_ALIGN);
332     if (space <= reserve)
333         return 0;
334     return space - reserve;
335 }
336
337 // Find the data block allocated with pmm_malloc with a given handle.
338 static void *
339 pmm_find(u32 handle)
340 {
341     struct pmmalloc_s *info;
342     for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
343         if (GET_PMMVAR(info->handle) == handle)
344             return GET_PMMVAR(info->data);
345     return NULL;
346 }
347
348
349 /****************************************************************
350  * pmm interface
351  ****************************************************************/
352
353 struct pmmheader {
354     u32 signature;
355     u8 version;
356     u8 length;
357     u8 checksum;
358     u16 entry_offset;
359     u16 entry_seg;
360     u8 reserved[5];
361 } PACKED;
362
363 extern struct pmmheader PMMHEADER;
364
365 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
366
367 #if CONFIG_PMM
368 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
369     .version = 0x01,
370     .length = sizeof(PMMHEADER),
371     .entry_seg = SEG_BIOS,
372 };
373 #endif
374
375 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
376
377 // PMM - allocate
378 static u32
379 handle_pmm00(u16 *args)
380 {
381     u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
382     u16 flags = args[5];
383     dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
384             , length, handle, flags);
385     struct zone_s *lowzone = &ZoneTmpLow, *highzone = &ZoneTmpHigh;
386     if (flags & 8) {
387         // Permanent memory request.
388         lowzone = &ZoneLow;
389         highzone = &ZoneHigh;
390     }
391     if (!length) {
392         // Memory size request
393         switch (flags & 3) {
394         default:
395         case 0:
396             return 0;
397         case 1:
398             return pmm_getspace(lowzone);
399         case 2:
400             return pmm_getspace(highzone);
401         case 3: {
402             u32 spacelow = pmm_getspace(lowzone);
403             u32 spacehigh = pmm_getspace(highzone);
404             if (spacelow > spacehigh)
405                 return spacelow;
406             return spacehigh;
407         }
408         }
409     }
410     u32 size = length * 16;
411     if ((s32)size <= 0)
412         return 0;
413     u32 align = MALLOC_MIN_ALIGN;
414     if (flags & 4) {
415         align = 1<<__ffs(size);
416         if (align < MALLOC_MIN_ALIGN)
417             align = MALLOC_MIN_ALIGN;
418     }
419     switch (flags & 3) {
420     default:
421     case 0:
422         return 0;
423     case 1:
424         return (u32)pmm_malloc(lowzone, handle, size, align);
425     case 2:
426         return (u32)pmm_malloc(highzone, handle, size, align);
427     case 3: {
428         void *data = pmm_malloc(lowzone, handle, size, align);
429         if (data)
430             return (u32)data;
431         return (u32)pmm_malloc(highzone, handle, size, align);
432     }
433     }
434 }
435
436 // PMM - find
437 static u32
438 handle_pmm01(u16 *args)
439 {
440     u32 handle = *(u32*)&args[1];
441     dprintf(3, "pmm01: handle=%x\n", handle);
442     if (handle == 0xFFFFFFFF)
443         return 0;
444     return (u32)pmm_find(handle);
445 }
446
447 // PMM - deallocate
448 static u32
449 handle_pmm02(u16 *args)
450 {
451     u32 buffer = *(u32*)&args[1];
452     dprintf(3, "pmm02: buffer=%x\n", buffer);
453     int ret = pmm_free((void*)buffer);
454     if (ret)
455         // Error
456         return 1;
457     return 0;
458 }
459
460 static u32
461 handle_pmmXX(u16 *args)
462 {
463     return PMM_FUNCTION_NOT_SUPPORTED;
464 }
465
466 u32 VISIBLE16
467 handle_pmm(u16 *args)
468 {
469     if (! CONFIG_PMM)
470         return PMM_FUNCTION_NOT_SUPPORTED;
471
472     u16 arg1 = args[0];
473     dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
474
475     switch (arg1) {
476     case 0x00: return handle_pmm00(args);
477     case 0x01: return handle_pmm01(args);
478     case 0x02: return handle_pmm02(args);
479     default:   return handle_pmmXX(args);
480     }
481 }
482
483 // romlayout.S
484 extern void entry_pmm();
485
486 void
487 pmm_setup()
488 {
489     if (! CONFIG_PMM)
490         return;
491
492     dprintf(3, "init PMM\n");
493
494     PMMAllocs = NULL;
495
496     PMMHEADER.signature = PMM_SIGNATURE;
497     PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
498     PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
499 }
500
501 void
502 pmm_finalize()
503 {
504     if (! CONFIG_PMM)
505         return;
506
507     dprintf(3, "finalize PMM\n");
508
509     PMMHEADER.signature = 0;
510     PMMHEADER.entry_offset = 0;
511 }