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;
222 malloc_finalize(void)
225 dprintf(3, "malloc finalize\n");
227 // Reserve more low-mem if needed.
228 u32 endlow = GET_BDA(mem_size_kb)*1024;
229 add_e820(endlow, BUILD_LOWRAM_END-endlow, E820_RESERVED);
231 // Give back unused high ram.
232 struct allocinfo_s *info = findLast(&ZoneHigh);
234 u32 giveback = ALIGN_DOWN(info->allocend - info->dataend, PAGE_SIZE);
235 add_e820((u32)info->dataend, giveback, E820_RAM);
236 dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
241 /****************************************************************
243 ****************************************************************/
247 relocate_ebda(u32 newebda, u32 oldebda, u8 ebda_size)
249 u32 lowram = GET_BDA(mem_size_kb) * 1024;
250 if (oldebda != lowram)
251 // EBDA isn't at end of ram - give up.
255 memmove((void*)newebda, (void*)oldebda, ebda_size * 1024);
258 dprintf(1, "ebda moved from %x to %x\n", oldebda, newebda);
259 SET_BDA(mem_size_kb, newebda / 1024);
260 SET_BDA(ebda_seg, FLATPTR_TO_SEG(newebda));
264 // Support expanding the ZoneLow dynamically.
266 zonelow_expand(u32 size, u32 align)
268 struct allocinfo_s *info = findLast(&ZoneLow);
271 u32 oldpos = (u32)info->allocend;
272 u32 newpos = ALIGN_DOWN(oldpos - size, align);
273 u32 bottom = (u32)info->dataend;
274 if (newpos >= bottom && newpos <= oldpos)
275 // Space already present.
277 u16 ebda_seg = get_ebda_seg();
278 u32 ebda_pos = (u32)MAKE_FLATPTR(ebda_seg, 0);
279 u8 ebda_size = GET_EBDA2(ebda_seg, size);
280 u32 ebda_end = ebda_pos + ebda_size * 1024;
281 if (ebda_end != bottom)
282 // Something else is after ebda - can't use any existing space.
283 newpos = ALIGN_DOWN(ebda_end - size, align);
284 u32 newbottom = ALIGN_DOWN(newpos, 1024);
285 u32 newebda = ALIGN_DOWN(newbottom - ebda_size * 1024, 1024);
286 if (newebda < BUILD_EBDA_MINIMUM)
291 int ret = relocate_ebda(newebda, ebda_pos, ebda_size);
296 if (ebda_end == bottom) {
297 info->data = (void*)newbottom;
298 info->dataend = (void*)newbottom;
300 addSpace(&ZoneLow, (void*)newbottom, (void*)ebda_end);
303 // Check if can expand the given zone to fulfill an allocation
305 allocExpandSpace(struct zone_s *zone, u32 size, u32 align
306 , struct allocinfo_s *fill)
308 void *data = allocSpace(zone, size, align, fill);
309 if (data || zone != &ZoneLow)
312 // Make sure to not move ebda while an optionrom is running.
313 if (unlikely(wait_preempt())) {
314 data = allocSpace(zone, size, align, fill);
319 zonelow_expand(size, align);
320 return allocSpace(zone, size, align, fill);
324 /****************************************************************
325 * tracked memory allocations
326 ****************************************************************/
328 // Allocate memory from the given zone and track it as a PMM allocation
330 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
336 // Find and reserve space for bookkeeping.
337 struct allocdetail_s *detail = allocSpace(
338 &ZoneTmpHigh, sizeof(*detail), MALLOC_MIN_ALIGN, NULL);
340 detail = allocSpace(&ZoneTmpLow, sizeof(*detail)
341 , MALLOC_MIN_ALIGN, NULL);
346 // Find and reserve space for main allocation
347 void *data = allocExpandSpace(zone, size, align, &detail->datainfo);
349 freeSpace(&detail->detailinfo);
353 dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
354 " ret=%p (detail=%p)\n"
355 , zone, handle, size, align
357 detail->handle = handle;
362 // Free a data block allocated with pmm_malloc
367 struct allocinfo_s *info = findAlloc(data);
368 if (!info || data == (void*)info || data == info->dataend)
370 struct allocdetail_s *detail = container_of(
371 info, struct allocdetail_s, datainfo);
372 dprintf(8, "pmm_free %p (detail=%p)\n", data, detail);
374 freeSpace(&detail->detailinfo);
378 // Find the amount of free space in a given zone.
380 pmm_getspace(struct zone_s *zone)
382 // XXX - doesn't account for ZoneLow being able to grow.
383 // XXX - results not reliable when CONFIG_THREAD_OPTIONROMS
385 struct allocinfo_s *info;
386 for (info = zone->info; info; info = info->next) {
387 u32 space = info->allocend - info->dataend;
388 if (space > maxspace)
392 if (zone != &ZoneTmpHigh && zone != &ZoneTmpLow)
394 // Account for space needed for PMM tracking.
395 u32 reserve = ALIGN(sizeof(struct allocdetail_s), MALLOC_MIN_ALIGN);
396 if (maxspace <= reserve)
398 return maxspace - reserve;
401 // Find the data block allocated with pmm_malloc with a given handle.
406 for (i=0; i<ARRAY_SIZE(Zones); i++) {
407 struct zone_s *zone = Zones[i];
408 struct allocinfo_s *info;
409 for (info = zone->info; info; info = info->next) {
410 if (info->data != (void*)info)
412 struct allocdetail_s *detail = container_of(
413 info, struct allocdetail_s, detailinfo);
414 if (detail->handle == handle)
415 return detail->datainfo.data;
422 /****************************************************************
424 ****************************************************************/
436 extern struct pmmheader PMMHEADER;
438 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
441 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
443 .length = sizeof(PMMHEADER),
444 .entry_seg = SEG_BIOS,
448 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
452 handle_pmm00(u16 *args)
454 u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
456 dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
457 , length, handle, flags);
458 struct zone_s *lowzone = &ZoneTmpLow, *highzone = &ZoneTmpHigh;
460 // Permanent memory request.
462 highzone = &ZoneHigh;
465 // Memory size request
471 return pmm_getspace(lowzone);
473 return pmm_getspace(highzone);
475 u32 spacelow = pmm_getspace(lowzone);
476 u32 spacehigh = pmm_getspace(highzone);
477 if (spacelow > spacehigh)
483 u32 size = length * 16;
486 u32 align = MALLOC_MIN_ALIGN;
488 align = 1<<__ffs(size);
489 if (align < MALLOC_MIN_ALIGN)
490 align = MALLOC_MIN_ALIGN;
497 return (u32)pmm_malloc(lowzone, handle, size, align);
499 return (u32)pmm_malloc(highzone, handle, size, align);
501 void *data = pmm_malloc(lowzone, handle, size, align);
504 return (u32)pmm_malloc(highzone, handle, size, align);
511 handle_pmm01(u16 *args)
513 u32 handle = *(u32*)&args[1];
514 dprintf(3, "pmm01: handle=%x\n", handle);
515 if (handle == PMM_DEFAULT_HANDLE)
517 return (u32)pmm_find(handle);
522 handle_pmm02(u16 *args)
524 u32 buffer = *(u32*)&args[1];
525 dprintf(3, "pmm02: buffer=%x\n", buffer);
526 int ret = pmm_free((void*)buffer);
534 handle_pmmXX(u16 *args)
536 return PMM_FUNCTION_NOT_SUPPORTED;
540 handle_pmm(u16 *args)
544 return PMM_FUNCTION_NOT_SUPPORTED;
547 dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
550 if (CONFIG_THREAD_OPTIONROMS) {
551 // Not a preemption event - don't wait in wait_preempt()
552 oldpreempt = CanPreempt;
558 case 0x00: ret = handle_pmm00(args); break;
559 case 0x01: ret = handle_pmm01(args); break;
560 case 0x02: ret = handle_pmm02(args); break;
561 default: ret = handle_pmmXX(args); break;
564 if (CONFIG_THREAD_OPTIONROMS)
565 CanPreempt = oldpreempt;
571 extern void entry_pmm(void);
579 dprintf(3, "init PMM\n");
581 PMMHEADER.signature = PMM_SIGNATURE;
582 PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
583 PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
592 dprintf(3, "finalize PMM\n");
594 PMMHEADER.signature = 0;
595 PMMHEADER.entry_offset = 0;