1 // Post memory manager (PMM) calls
3 // Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
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
13 // Information on a reserved area.
15 struct allocinfo_s *next, **pprev;
16 void *data, *dataend, *allocend;
19 // Information on a tracked memory allocation.
20 struct allocdetail_s {
21 struct allocinfo_s detailinfo;
22 struct allocinfo_s datainfo;
26 // The various memory zones.
28 struct allocinfo_s *info;
31 struct zone_s ZoneLow, ZoneHigh, ZoneFSeg, ZoneTmpLow, ZoneTmpHigh;
33 static struct zone_s *Zones[] = {
34 &ZoneTmpLow, &ZoneLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
38 /****************************************************************
39 * low-level memory reservations
40 ****************************************************************/
42 // Find and reserve space from a given zone
44 allocSpace(struct zone_s *zone, u32 size, u32 align, struct allocinfo_s *fill)
46 struct allocinfo_s *info;
47 for (info = zone->info; info; info = info->next) {
48 void *dataend = info->dataend;
49 void *allocend = info->allocend;
50 void *newallocend = (void*)ALIGN_DOWN((u32)allocend - size, align);
51 if (newallocend >= dataend && newallocend <= allocend) {
52 // Found space - now reserve it.
53 struct allocinfo_s **pprev = info->pprev;
58 fill->data = newallocend;
59 fill->dataend = newallocend + size;
60 fill->allocend = allocend;
62 info->allocend = newallocend;
63 info->pprev = &fill->next;
71 // Release space allocated with allocSpace()
73 freeSpace(struct allocinfo_s *info)
75 struct allocinfo_s *next = info->next;
76 struct allocinfo_s **pprev = info->pprev;
79 if (next->allocend == info->data)
80 next->allocend = info->allocend;
85 // Add new memory to a zone
87 addSpace(struct zone_s *zone, void *start, void *end)
89 // Find position to add space
90 struct allocinfo_s **pprev = &zone->info, *info;
93 if (!info || info->data < start)
98 // Add space using temporary allocation info.
99 struct allocdetail_s tempdetail;
100 tempdetail.datainfo.next = info;
101 tempdetail.datainfo.pprev = pprev;
102 tempdetail.datainfo.data = tempdetail.datainfo.dataend = start;
103 tempdetail.datainfo.allocend = end;
104 *pprev = &tempdetail.datainfo;
106 info->pprev = &tempdetail.datainfo.next;
108 // Allocate final allocation info.
109 struct allocdetail_s *detail = allocSpace(
110 &ZoneTmpHigh, sizeof(*detail), MALLOC_MIN_ALIGN, NULL);
112 detail = allocSpace(&ZoneTmpLow, sizeof(*detail)
113 , MALLOC_MIN_ALIGN, NULL);
115 *tempdetail.datainfo.pprev = tempdetail.datainfo.next;
116 if (tempdetail.datainfo.next)
117 tempdetail.datainfo.next->pprev = tempdetail.datainfo.pprev;
123 // Replace temp alloc space with final alloc space
124 memcpy(&detail->datainfo, &tempdetail.datainfo, sizeof(detail->datainfo));
125 detail->handle = PMM_DEFAULT_HANDLE;
127 *tempdetail.datainfo.pprev = &detail->datainfo;
128 if (tempdetail.datainfo.next)
129 tempdetail.datainfo.next->pprev = &detail->datainfo.next;
132 // Search all zones for an allocation obtained from allocSpace()
133 static struct allocinfo_s *
134 findAlloc(void *data)
137 for (i=0; i<ARRAY_SIZE(Zones); i++) {
138 struct zone_s *zone = Zones[i];
139 struct allocinfo_s *info;
140 for (info = zone->info; info; info = info->next)
141 if (info->data == data)
147 // Return the last sentinal node of a zone
148 static struct allocinfo_s *
149 findLast(struct zone_s *zone)
151 struct allocinfo_s *info = zone->info;
155 struct allocinfo_s *next = info->next;
163 /****************************************************************
165 ****************************************************************/
171 dprintf(3, "malloc setup\n");
173 // Populate temp high ram
176 for (i=e820_count-1; i>=0; i--) {
177 struct e820entry *en = &e820_list[i];
178 u64 end = en->start + en->size;
181 if (en->type != E820_RAM || end > 0xffffffff)
183 u32 s = en->start, e = end;
185 u32 newe = ALIGN_DOWN(e - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
186 if (newe <= e && newe >= s) {
191 addSpace(&ZoneTmpHigh, (void*)s, (void*)e);
194 // Populate other regions
195 addSpace(&ZoneTmpLow, (void*)BUILD_STACK_ADDR, (void*)BUILD_EBDA_MINIMUM);
196 addSpace(&ZoneFSeg, BiosTableSpace, &BiosTableSpace[CONFIG_MAX_BIOSTABLE]);
197 addSpace(&ZoneLow, (void*)BUILD_LOWRAM_END, (void*)BUILD_LOWRAM_END);
199 addSpace(&ZoneHigh, (void*)highram
200 , (void*)highram + CONFIG_MAX_HIGHTABLE);
201 add_e820(highram, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
205 // Update pointers after code relocation.
207 malloc_fixupreloc(void)
210 if (!CONFIG_RELOCATE_INIT)
212 dprintf(3, "malloc fixup reloc\n");
215 for (i=0; i<ARRAY_SIZE(Zones); i++) {
216 struct zone_s *zone = Zones[i];
217 zone->info->pprev = &zone->info;
220 // Add space free'd during relocation in f-segment to ZoneFSeg
221 extern u8 code32init_end[];
222 if ((u32)code32init_end > BUILD_BIOS_ADDR) {
223 memset((void*)BUILD_BIOS_ADDR, 0, (u32)code32init_end - BUILD_BIOS_ADDR);
224 addSpace(&ZoneFSeg, (void*)BUILD_BIOS_ADDR, code32init_end);
229 malloc_finalize(void)
232 dprintf(3, "malloc finalize\n");
234 // Reserve more low-mem if needed.
235 u32 endlow = GET_BDA(mem_size_kb)*1024;
236 add_e820(endlow, BUILD_LOWRAM_END-endlow, E820_RESERVED);
238 // Give back unused high ram.
239 struct allocinfo_s *info = findLast(&ZoneHigh);
241 u32 giveback = ALIGN_DOWN(info->allocend - info->dataend, PAGE_SIZE);
242 add_e820((u32)info->dataend, giveback, E820_RAM);
243 dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
248 /****************************************************************
250 ****************************************************************/
254 relocate_ebda(u32 newebda, u32 oldebda, u8 ebda_size)
256 u32 lowram = GET_BDA(mem_size_kb) * 1024;
257 if (oldebda != lowram)
258 // EBDA isn't at end of ram - give up.
262 memmove((void*)newebda, (void*)oldebda, ebda_size * 1024);
265 dprintf(1, "ebda moved from %x to %x\n", oldebda, newebda);
266 SET_BDA(mem_size_kb, newebda / 1024);
267 SET_BDA(ebda_seg, FLATPTR_TO_SEG(newebda));
271 // Support expanding the ZoneLow dynamically.
273 zonelow_expand(u32 size, u32 align)
275 struct allocinfo_s *info = findLast(&ZoneLow);
278 u32 oldpos = (u32)info->allocend;
279 u32 newpos = ALIGN_DOWN(oldpos - size, align);
280 u32 bottom = (u32)info->dataend;
281 if (newpos >= bottom && newpos <= oldpos)
282 // Space already present.
284 u16 ebda_seg = get_ebda_seg();
285 u32 ebda_pos = (u32)MAKE_FLATPTR(ebda_seg, 0);
286 u8 ebda_size = GET_EBDA2(ebda_seg, size);
287 u32 ebda_end = ebda_pos + ebda_size * 1024;
288 if (ebda_end != bottom)
289 // Something else is after ebda - can't use any existing space.
290 newpos = ALIGN_DOWN(ebda_end - size, align);
291 u32 newbottom = ALIGN_DOWN(newpos, 1024);
292 u32 newebda = ALIGN_DOWN(newbottom - ebda_size * 1024, 1024);
293 if (newebda < BUILD_EBDA_MINIMUM)
298 int ret = relocate_ebda(newebda, ebda_pos, ebda_size);
303 if (ebda_end == bottom) {
304 info->data = (void*)newbottom;
305 info->dataend = (void*)newbottom;
307 addSpace(&ZoneLow, (void*)newbottom, (void*)ebda_end);
310 // Check if can expand the given zone to fulfill an allocation
312 allocExpandSpace(struct zone_s *zone, u32 size, u32 align
313 , struct allocinfo_s *fill)
315 void *data = allocSpace(zone, size, align, fill);
316 if (data || zone != &ZoneLow)
319 // Make sure to not move ebda while an optionrom is running.
320 if (unlikely(wait_preempt())) {
321 data = allocSpace(zone, size, align, fill);
326 zonelow_expand(size, align);
327 return allocSpace(zone, size, align, fill);
331 /****************************************************************
332 * tracked memory allocations
333 ****************************************************************/
335 // Allocate memory from the given zone and track it as a PMM allocation
337 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
343 // Find and reserve space for bookkeeping.
344 struct allocdetail_s *detail = allocSpace(
345 &ZoneTmpHigh, sizeof(*detail), MALLOC_MIN_ALIGN, NULL);
347 detail = allocSpace(&ZoneTmpLow, sizeof(*detail)
348 , MALLOC_MIN_ALIGN, NULL);
353 // Find and reserve space for main allocation
354 void *data = allocExpandSpace(zone, size, align, &detail->datainfo);
356 freeSpace(&detail->detailinfo);
360 dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
361 " ret=%p (detail=%p)\n"
362 , zone, handle, size, align
364 detail->handle = handle;
369 // Free a data block allocated with pmm_malloc
374 struct allocinfo_s *info = findAlloc(data);
375 if (!info || data == (void*)info || data == info->dataend)
377 struct allocdetail_s *detail = container_of(
378 info, struct allocdetail_s, datainfo);
379 dprintf(8, "pmm_free %p (detail=%p)\n", data, detail);
381 freeSpace(&detail->detailinfo);
385 // Find the amount of free space in a given zone.
387 pmm_getspace(struct zone_s *zone)
389 // XXX - doesn't account for ZoneLow being able to grow.
390 // XXX - results not reliable when CONFIG_THREAD_OPTIONROMS
392 struct allocinfo_s *info;
393 for (info = zone->info; info; info = info->next) {
394 u32 space = info->allocend - info->dataend;
395 if (space > maxspace)
399 if (zone != &ZoneTmpHigh && zone != &ZoneTmpLow)
401 // Account for space needed for PMM tracking.
402 u32 reserve = ALIGN(sizeof(struct allocdetail_s), MALLOC_MIN_ALIGN);
403 if (maxspace <= reserve)
405 return maxspace - reserve;
408 // Find the data block allocated with pmm_malloc with a given handle.
413 for (i=0; i<ARRAY_SIZE(Zones); i++) {
414 struct zone_s *zone = Zones[i];
415 struct allocinfo_s *info;
416 for (info = zone->info; info; info = info->next) {
417 if (info->data != (void*)info)
419 struct allocdetail_s *detail = container_of(
420 info, struct allocdetail_s, detailinfo);
421 if (detail->handle == handle)
422 return detail->datainfo.data;
429 /****************************************************************
431 ****************************************************************/
443 extern struct pmmheader PMMHEADER;
445 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
448 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
450 .length = sizeof(PMMHEADER),
451 .entry_seg = SEG_BIOS,
455 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
459 handle_pmm00(u16 *args)
461 u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
463 dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
464 , length, handle, flags);
465 struct zone_s *lowzone = &ZoneTmpLow, *highzone = &ZoneTmpHigh;
467 // Permanent memory request.
469 highzone = &ZoneHigh;
472 // Memory size request
478 return pmm_getspace(lowzone);
480 return pmm_getspace(highzone);
482 u32 spacelow = pmm_getspace(lowzone);
483 u32 spacehigh = pmm_getspace(highzone);
484 if (spacelow > spacehigh)
490 u32 size = length * 16;
493 u32 align = MALLOC_MIN_ALIGN;
495 align = 1<<__ffs(size);
496 if (align < MALLOC_MIN_ALIGN)
497 align = MALLOC_MIN_ALIGN;
504 return (u32)pmm_malloc(lowzone, handle, size, align);
506 return (u32)pmm_malloc(highzone, handle, size, align);
508 void *data = pmm_malloc(lowzone, handle, size, align);
511 return (u32)pmm_malloc(highzone, handle, size, align);
518 handle_pmm01(u16 *args)
520 u32 handle = *(u32*)&args[1];
521 dprintf(3, "pmm01: handle=%x\n", handle);
522 if (handle == PMM_DEFAULT_HANDLE)
524 return (u32)pmm_find(handle);
529 handle_pmm02(u16 *args)
531 u32 buffer = *(u32*)&args[1];
532 dprintf(3, "pmm02: buffer=%x\n", buffer);
533 int ret = pmm_free((void*)buffer);
541 handle_pmmXX(u16 *args)
543 return PMM_FUNCTION_NOT_SUPPORTED;
547 handle_pmm(u16 *args)
551 return PMM_FUNCTION_NOT_SUPPORTED;
554 dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
557 if (CONFIG_THREAD_OPTIONROMS) {
558 // Not a preemption event - don't wait in wait_preempt()
559 oldpreempt = CanPreempt;
565 case 0x00: ret = handle_pmm00(args); break;
566 case 0x01: ret = handle_pmm01(args); break;
567 case 0x02: ret = handle_pmm02(args); break;
568 default: ret = handle_pmmXX(args); break;
571 if (CONFIG_THREAD_OPTIONROMS)
572 CanPreempt = oldpreempt;
578 extern void entry_pmm(void);
586 dprintf(3, "init PMM\n");
588 PMMHEADER.signature = PMM_SIGNATURE;
589 PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
590 PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
599 dprintf(3, "finalize PMM\n");
601 PMMHEADER.signature = 0;
602 PMMHEADER.entry_offset = 0;