Rework malloc to use a "first fit" algorithm.
[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" // struct e820entry
10 #include "farptr.h" // GET_FARVAR
11 #include "biosvar.h" // GET_BDA
12
13
14 #if MODESEGMENT
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) ({                      \
18             SET_SEG(ES, 0);                     \
19             __GET_VAR("addr32 ", ES, (var)); })
20 #define SET_PMMVAR(var, val) do {               \
21         SET_SEG(ES, 0);                         \
22         __SET_VAR("addr32 ", ES, (var), (val)); \
23     } while (0)
24 #else
25 #define GET_PMMVAR(var) (var)
26 #define SET_PMMVAR(var, val) do { (var) = (val); } while (0)
27 #endif
28
29 // Information on a reserved area.
30 struct allocinfo_s {
31     struct allocinfo_s *next, **pprev;
32     void *data, *dataend, *allocend;
33 };
34
35 // Information on a tracked memory allocation.
36 struct allocdetail_s {
37     struct allocinfo_s detailinfo;
38     struct allocinfo_s datainfo;
39     u32 handle;
40 };
41
42 // The various memory zones.
43 struct zone_s {
44     struct allocinfo_s *info;
45 };
46
47 struct zone_s ZoneLow VAR32FLATVISIBLE;
48 struct zone_s ZoneHigh VAR32FLATVISIBLE;
49 struct zone_s ZoneFSeg VAR32FLATVISIBLE;
50 struct zone_s ZoneTmpLow VAR32FLATVISIBLE;
51 struct zone_s ZoneTmpHigh VAR32FLATVISIBLE;
52
53 struct zone_s *Zones[] VAR32FLATVISIBLE = {
54     &ZoneTmpLow, &ZoneLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
55 };
56
57
58 /****************************************************************
59  * low-level memory reservations
60  ****************************************************************/
61
62 // Find and reserve space from a given zone
63 static void *
64 allocSpace(struct zone_s *zone, u32 size, u32 align, struct allocinfo_s *fill)
65 {
66     struct allocinfo_s *info;
67     for (info = GET_PMMVAR(zone->info); info; info = GET_PMMVAR(info->next)) {
68         void *dataend = GET_PMMVAR(info->dataend);
69         void *allocend = GET_PMMVAR(info->allocend);
70         void *newallocend = (void*)ALIGN_DOWN((u32)allocend - size, align);
71         if (newallocend >= dataend && newallocend <= allocend) {
72             // Found space - now reserve it.
73             struct allocinfo_s **pprev = GET_PMMVAR(info->pprev);
74             if (!fill)
75                 fill = newallocend;
76             SET_PMMVAR(fill->next, info);
77             SET_PMMVAR(fill->pprev, pprev);
78             SET_PMMVAR(fill->data, newallocend);
79             SET_PMMVAR(fill->dataend, newallocend + size);
80             SET_PMMVAR(fill->allocend, allocend);
81
82             SET_PMMVAR(info->allocend, newallocend);
83             SET_PMMVAR(info->pprev, &fill->next);
84             SET_PMMVAR(*pprev, fill);
85             return newallocend;
86         }
87     }
88     return NULL;
89 }
90
91 // Release space allocated with allocSpace()
92 static void
93 freeSpace(struct allocinfo_s *info)
94 {
95     struct allocinfo_s *next = GET_PMMVAR(info->next);
96     struct allocinfo_s **pprev = GET_PMMVAR(info->pprev);
97     SET_PMMVAR(*pprev, next);
98     if (next) {
99         if (GET_PMMVAR(next->allocend) == GET_PMMVAR(info->data))
100             SET_PMMVAR(next->allocend, GET_PMMVAR(info->allocend));
101         SET_PMMVAR(next->pprev, pprev);
102     }
103 }
104
105 // Add new memory to a zone
106 static void
107 addSpace(struct zone_s *zone, void *start, void *end)
108 {
109     // Find position to add space
110     struct allocinfo_s **pprev = &zone->info, *info;
111     for (;;) {
112         info = GET_PMMVAR(*pprev);
113         if (!info || GET_PMMVAR(info->data) < start)
114             break;
115         pprev = &info->next;
116     }
117
118     // Add space using temporary allocation info.
119     struct allocdetail_s tempdetail;
120     tempdetail.datainfo.next = info;
121     tempdetail.datainfo.pprev = pprev;
122     tempdetail.datainfo.data = tempdetail.datainfo.dataend = start;
123     tempdetail.datainfo.allocend = end;
124     tempdetail.handle = PMM_DEFAULT_HANDLE;
125     struct allocdetail_s *tempdetailp = MAKE_FLATPTR(GET_SEG(SS), &tempdetail);
126     SET_PMMVAR(*pprev, &tempdetailp->datainfo);
127     if (info)
128         SET_PMMVAR(info->pprev, &tempdetailp->datainfo.next);
129
130     // Allocate final allocation info.
131     struct allocdetail_s *detail = allocSpace(
132         &ZoneTmpHigh, sizeof(*detail), MALLOC_MIN_ALIGN, NULL);
133     if (!detail) {
134         detail = allocSpace(&ZoneTmpLow, sizeof(*detail)
135                             , MALLOC_MIN_ALIGN, NULL);
136         if (!detail) {
137             SET_PMMVAR(*tempdetail.datainfo.pprev, tempdetail.datainfo.next);
138             if (tempdetail.datainfo.next)
139                 SET_PMMVAR(tempdetail.datainfo.next->pprev
140                            , tempdetail.datainfo.pprev);
141             warn_noalloc();
142             return;
143         }
144     }
145
146     // Replace temp alloc space with final alloc space
147     SET_PMMVAR(detail->datainfo.next, tempdetail.datainfo.next);
148     SET_PMMVAR(detail->datainfo.pprev, tempdetail.datainfo.pprev);
149     SET_PMMVAR(detail->datainfo.data, tempdetail.datainfo.data);
150     SET_PMMVAR(detail->datainfo.dataend, tempdetail.datainfo.dataend);
151     SET_PMMVAR(detail->datainfo.allocend, tempdetail.datainfo.allocend);
152     SET_PMMVAR(detail->handle, PMM_DEFAULT_HANDLE);
153
154     SET_PMMVAR(*tempdetail.datainfo.pprev, &detail->datainfo);
155     if (tempdetail.datainfo.next)
156         SET_PMMVAR(tempdetail.datainfo.next->pprev, &detail->datainfo.next);
157 }
158
159 // Search all zones for an allocation obtained from allocSpace()
160 static struct allocinfo_s *
161 findAlloc(void *data)
162 {
163     int i;
164     for (i=0; i<ARRAY_SIZE(Zones); i++) {
165         struct zone_s *zone = GET_PMMVAR(Zones[i]);
166         struct allocinfo_s *info;
167         for (info = GET_PMMVAR(zone->info); info; info = GET_PMMVAR(info->next))
168             if (GET_PMMVAR(info->data) == data)
169                 return info;
170     }
171     return NULL;
172 }
173
174 // Return the last sentinal node of a zone
175 static struct allocinfo_s *
176 findLast(struct zone_s *zone)
177 {
178     struct allocinfo_s *info = GET_PMMVAR(zone->info);
179     if (!info)
180         return NULL;
181     for (;;) {
182         struct allocinfo_s *next = GET_PMMVAR(info->next);
183         if (!next)
184             return info;
185         info = next;
186     }
187 }
188
189
190 /****************************************************************
191  * Setup
192  ****************************************************************/
193
194 void
195 malloc_setup(void)
196 {
197     ASSERT32FLAT();
198     dprintf(3, "malloc setup\n");
199
200     ZoneLow.info = ZoneHigh.info = ZoneFSeg.info = NULL;
201     ZoneTmpLow.info = ZoneTmpHigh.info = NULL;
202
203     // Clear memory in 0xf0000 area.
204     extern u8 code32flat_start[];
205     if ((u32)code32flat_start > BUILD_BIOS_ADDR)
206         // Clear unused parts of f-segment
207         memset((void*)BUILD_BIOS_ADDR, 0
208                , (u32)code32flat_start - BUILD_BIOS_ADDR);
209     memset(BiosTableSpace, 0, CONFIG_MAX_BIOSTABLE);
210
211     // Populate temp high ram
212     u32 highram = 0;
213     int i;
214     for (i=e820_count-1; i>=0; i--) {
215         struct e820entry *en = &e820_list[i];
216         u64 end = en->start + en->size;
217         if (end < 1024*1024)
218             break;
219         if (en->type != E820_RAM || end > 0xffffffff)
220             continue;
221         u32 s = en->start, e = end;
222         if (!highram) {
223             u32 newe = ALIGN_DOWN(e - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
224             if (newe <= e && newe >= s) {
225                 highram = newe;
226                 e = newe;
227             }
228         }
229         addSpace(&ZoneTmpHigh, (void*)s, (void*)e);
230     }
231
232     // Populate other regions
233     addSpace(&ZoneTmpLow, (void*)BUILD_STACK_ADDR, (void*)BUILD_EBDA_MINIMUM);
234     addSpace(&ZoneFSeg, BiosTableSpace, &BiosTableSpace[CONFIG_MAX_BIOSTABLE]);
235     addSpace(&ZoneLow, (void*)BUILD_LOWRAM_END, (void*)BUILD_LOWRAM_END);
236     if (highram) {
237         addSpace(&ZoneHigh, (void*)highram
238                  , (void*)highram + CONFIG_MAX_HIGHTABLE);
239         add_e820(highram, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
240     }
241 }
242
243 void
244 malloc_finalize(void)
245 {
246     dprintf(3, "malloc finalize\n");
247
248     // Reserve more low-mem if needed.
249     u32 endlow = GET_BDA(mem_size_kb)*1024;
250     add_e820(endlow, BUILD_LOWRAM_END-endlow, E820_RESERVED);
251
252     // Give back unused high ram.
253     struct allocinfo_s *info = findLast(&ZoneHigh);
254     if (info) {
255         u32 giveback = ALIGN_DOWN(info->allocend - info->dataend, PAGE_SIZE);
256         add_e820((u32)info->dataend, giveback, E820_RAM);
257         dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
258     }
259
260     // Clear low-memory allocations.
261     memset((void*)BUILD_STACK_ADDR, 0, BUILD_EBDA_MINIMUM - BUILD_STACK_ADDR);
262 }
263
264
265 /****************************************************************
266  * ebda movement
267  ****************************************************************/
268
269 // Move ebda
270 static int
271 relocate_ebda(u32 newebda, u32 oldebda, u8 ebda_size)
272 {
273     u32 lowram = GET_BDA(mem_size_kb) * 1024;
274     if (oldebda != lowram)
275         // EBDA isn't at end of ram - give up.
276         return -1;
277
278     // Do copy
279     if (MODESEGMENT)
280         memcpy_far(FLATPTR_TO_SEG(newebda)
281                    , (void*)FLATPTR_TO_OFFSET(newebda)
282                    , FLATPTR_TO_SEG(oldebda)
283                    , (void*)FLATPTR_TO_OFFSET(oldebda)
284                    , ebda_size * 1024);
285     else
286         memmove((void*)newebda, (void*)oldebda, ebda_size * 1024);
287
288     // Update indexes
289     dprintf(1, "ebda moved from %x to %x\n", oldebda, newebda);
290     SET_BDA(mem_size_kb, newebda / 1024);
291     SET_BDA(ebda_seg, FLATPTR_TO_SEG(newebda));
292     return 0;
293 }
294
295 // Support expanding the ZoneLow dynamically.
296 static void
297 zonelow_expand(u32 size, u32 align)
298 {
299     struct allocinfo_s *info = findLast(&ZoneLow);
300     if (!info)
301         return;
302     u32 oldpos = (u32)GET_PMMVAR(info->allocend);
303     u32 newpos = ALIGN_DOWN(oldpos - size, align);
304     u32 bottom = (u32)GET_PMMVAR(info->dataend);
305     if (newpos >= bottom && newpos <= oldpos)
306         // Space already present.
307         return;
308     u16 ebda_seg = get_ebda_seg();
309     u32 ebda_pos = (u32)MAKE_FLATPTR(ebda_seg, 0);
310     u8 ebda_size = GET_EBDA2(ebda_seg, size);
311     u32 ebda_end = ebda_pos + ebda_size * 1024;
312     if (ebda_end != bottom)
313         // Something else is after ebda - can't use any existing space.
314         newpos = ALIGN_DOWN(ebda_end - size, align);
315     u32 newbottom = ALIGN_DOWN(newpos, 1024);
316     u32 newebda = ALIGN_DOWN(newbottom - ebda_size * 1024, 1024);
317     if (newebda < BUILD_EBDA_MINIMUM)
318         // Not enough space.
319         return;
320
321     // Move ebda
322     int ret = relocate_ebda(newebda, ebda_pos, ebda_size);
323     if (ret)
324         return;
325
326     // Update zone
327     if (ebda_end == bottom) {
328         SET_PMMVAR(info->data, (void*)newbottom);
329         SET_PMMVAR(info->dataend, (void*)newbottom);
330     } else
331         addSpace(&ZoneLow, (void*)newbottom, (void*)ebda_end);
332 }
333
334 // Check if can expand the given zone to fulfill an allocation
335 static void *
336 allocExpandSpace(struct zone_s *zone, u32 size, u32 align
337                  , struct allocinfo_s *fill)
338 {
339     void *data = allocSpace(zone, size, align, fill);
340     if (data || zone != &ZoneLow)
341         return data;
342
343     // Make sure to not move ebda while an optionrom is running.
344     if (unlikely(wait_preempt())) {
345         data = allocSpace(zone, size, align, fill);
346         if (data)
347             return data;
348     }
349
350     zonelow_expand(size, align);
351     return allocSpace(zone, size, align, fill);
352 }
353
354
355 /****************************************************************
356  * tracked memory allocations
357  ****************************************************************/
358
359 // Allocate memory from the given zone and track it as a PMM allocation
360 void * __malloc
361 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
362 {
363     if (!size)
364         return NULL;
365
366     // Find and reserve space for bookkeeping.
367     struct allocdetail_s *detail = allocSpace(
368         &ZoneTmpHigh, sizeof(*detail), MALLOC_MIN_ALIGN, NULL);
369     if (!detail) {
370         detail = allocSpace(&ZoneTmpLow, sizeof(*detail)
371                             , MALLOC_MIN_ALIGN, NULL);
372         if (!detail)
373             return NULL;
374     }
375
376     // Find and reserve space for main allocation
377     void *data = allocExpandSpace(zone, size, align, &detail->datainfo);
378     if (!data) {
379         freeSpace(&detail->detailinfo);
380         return NULL;
381     }
382
383     dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
384             " ret=%p (detail=%p)\n"
385             , zone, handle, size, align
386             , data, detail);
387     SET_PMMVAR(detail->handle, handle);
388
389     return data;
390 }
391
392 // Free a data block allocated with pmm_malloc
393 int
394 pmm_free(void *data)
395 {
396     struct allocinfo_s *info = findAlloc(data);
397     if (!info || data == (void*)info || data == GET_PMMVAR(info->dataend))
398         return -1;
399     struct allocdetail_s *detail = container_of(
400         info, struct allocdetail_s, datainfo);
401     dprintf(8, "pmm_free %p (detail=%p)\n", data, detail);
402     freeSpace(info);
403     freeSpace(&detail->detailinfo);
404     return 0;
405 }
406
407 // Find the amount of free space in a given zone.
408 static u32
409 pmm_getspace(struct zone_s *zone)
410 {
411     // XXX - doesn't account for ZoneLow being able to grow.
412     // XXX - results not reliable when CONFIG_THREAD_OPTIONROMS
413     u32 maxspace = 0;
414     struct allocinfo_s *info;
415     for (info = GET_PMMVAR(zone->info); info; info = GET_PMMVAR(info->next)) {
416         u32 space = GET_PMMVAR(info->allocend) - GET_PMMVAR(info->dataend);
417         if (space > maxspace)
418             maxspace = space;
419     }
420
421     if (zone != &ZoneTmpHigh && zone != &ZoneTmpLow)
422         return maxspace;
423     // Account for space needed for PMM tracking.
424     u32 reserve = ALIGN(sizeof(struct allocdetail_s), MALLOC_MIN_ALIGN);
425     if (maxspace <= reserve)
426         return 0;
427     return maxspace - reserve;
428 }
429
430 // Find the data block allocated with pmm_malloc with a given handle.
431 static void *
432 pmm_find(u32 handle)
433 {
434     int i;
435     for (i=0; i<ARRAY_SIZE(Zones); i++) {
436         struct zone_s *zone = GET_PMMVAR(Zones[i]);
437         struct allocinfo_s *info;
438         for (info = GET_PMMVAR(zone->info); info
439                  ; info = GET_PMMVAR(info->next)) {
440             if (GET_PMMVAR(info->data) != (void*)info)
441                 continue;
442             struct allocdetail_s *detail = container_of(
443                 info, struct allocdetail_s, detailinfo);
444             if (GET_PMMVAR(detail->handle) == handle)
445                 return GET_PMMVAR(detail->datainfo.data);
446         }
447     }
448     return NULL;
449 }
450
451
452 /****************************************************************
453  * pmm interface
454  ****************************************************************/
455
456 struct pmmheader {
457     u32 signature;
458     u8 version;
459     u8 length;
460     u8 checksum;
461     u16 entry_offset;
462     u16 entry_seg;
463     u8 reserved[5];
464 } PACKED;
465
466 extern struct pmmheader PMMHEADER;
467
468 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
469
470 #if CONFIG_PMM
471 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
472     .version = 0x01,
473     .length = sizeof(PMMHEADER),
474     .entry_seg = SEG_BIOS,
475 };
476 #endif
477
478 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
479
480 // PMM - allocate
481 static u32
482 handle_pmm00(u16 *args)
483 {
484     u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
485     u16 flags = args[5];
486     dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
487             , length, handle, flags);
488     struct zone_s *lowzone = &ZoneTmpLow, *highzone = &ZoneTmpHigh;
489     if (flags & 8) {
490         // Permanent memory request.
491         lowzone = &ZoneLow;
492         highzone = &ZoneHigh;
493     }
494     if (!length) {
495         // Memory size request
496         switch (flags & 3) {
497         default:
498         case 0:
499             return 0;
500         case 1:
501             return pmm_getspace(lowzone);
502         case 2:
503             return pmm_getspace(highzone);
504         case 3: {
505             u32 spacelow = pmm_getspace(lowzone);
506             u32 spacehigh = pmm_getspace(highzone);
507             if (spacelow > spacehigh)
508                 return spacelow;
509             return spacehigh;
510         }
511         }
512     }
513     u32 size = length * 16;
514     if ((s32)size <= 0)
515         return 0;
516     u32 align = MALLOC_MIN_ALIGN;
517     if (flags & 4) {
518         align = 1<<__ffs(size);
519         if (align < MALLOC_MIN_ALIGN)
520             align = MALLOC_MIN_ALIGN;
521     }
522     switch (flags & 3) {
523     default:
524     case 0:
525         return 0;
526     case 1:
527         return (u32)pmm_malloc(lowzone, handle, size, align);
528     case 2:
529         return (u32)pmm_malloc(highzone, handle, size, align);
530     case 3: {
531         void *data = pmm_malloc(lowzone, handle, size, align);
532         if (data)
533             return (u32)data;
534         return (u32)pmm_malloc(highzone, handle, size, align);
535     }
536     }
537 }
538
539 // PMM - find
540 static u32
541 handle_pmm01(u16 *args)
542 {
543     u32 handle = *(u32*)&args[1];
544     dprintf(3, "pmm01: handle=%x\n", handle);
545     if (handle == PMM_DEFAULT_HANDLE)
546         return 0;
547     return (u32)pmm_find(handle);
548 }
549
550 // PMM - deallocate
551 static u32
552 handle_pmm02(u16 *args)
553 {
554     u32 buffer = *(u32*)&args[1];
555     dprintf(3, "pmm02: buffer=%x\n", buffer);
556     int ret = pmm_free((void*)buffer);
557     if (ret)
558         // Error
559         return 1;
560     return 0;
561 }
562
563 static u32
564 handle_pmmXX(u16 *args)
565 {
566     return PMM_FUNCTION_NOT_SUPPORTED;
567 }
568
569 u32 VISIBLE16
570 handle_pmm(u16 *args)
571 {
572     if (! CONFIG_PMM)
573         return PMM_FUNCTION_NOT_SUPPORTED;
574
575     u16 arg1 = args[0];
576     dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
577
578     switch (arg1) {
579     case 0x00: return handle_pmm00(args);
580     case 0x01: return handle_pmm01(args);
581     case 0x02: return handle_pmm02(args);
582     default:   return handle_pmmXX(args);
583     }
584 }
585
586 // romlayout.S
587 extern void entry_pmm(void);
588
589 void
590 pmm_setup(void)
591 {
592     if (! CONFIG_PMM)
593         return;
594
595     dprintf(3, "init PMM\n");
596
597     PMMHEADER.signature = PMM_SIGNATURE;
598     PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
599     PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
600 }
601
602 void
603 pmm_finalize(void)
604 {
605     if (! CONFIG_PMM)
606         return;
607
608     dprintf(3, "finalize PMM\n");
609
610     PMMHEADER.signature = 0;
611     PMMHEADER.entry_offset = 0;
612 }