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
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) ({ \
19 __GET_VAR("addr32 ", ES, (var)); })
20 #define SET_PMMVAR(var, val) do { \
22 __SET_VAR("addr32 ", ES, (var), (val)); \
25 #define GET_PMMVAR(var) (var)
26 #define SET_PMMVAR(var, val) do { (var) = (val); } while (0)
29 // Information on a reserved area.
31 struct allocinfo_s *next, **pprev;
32 void *data, *dataend, *allocend;
35 // Information on a tracked memory allocation.
36 struct allocdetail_s {
37 struct allocinfo_s detailinfo;
38 struct allocinfo_s datainfo;
42 // The various memory zones.
44 struct allocinfo_s *info;
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;
53 struct zone_s *Zones[] VAR32FLATVISIBLE = {
54 &ZoneTmpLow, &ZoneLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
58 /****************************************************************
59 * low-level memory reservations
60 ****************************************************************/
62 // Find and reserve space from a given zone
64 allocSpace(struct zone_s *zone, u32 size, u32 align, struct allocinfo_s *fill)
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);
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);
82 SET_PMMVAR(info->allocend, newallocend);
83 SET_PMMVAR(info->pprev, &fill->next);
84 SET_PMMVAR(*pprev, fill);
91 // Release space allocated with allocSpace()
93 freeSpace(struct allocinfo_s *info)
95 struct allocinfo_s *next = GET_PMMVAR(info->next);
96 struct allocinfo_s **pprev = GET_PMMVAR(info->pprev);
97 SET_PMMVAR(*pprev, 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);
105 // Add new memory to a zone
107 addSpace(struct zone_s *zone, void *start, void *end)
109 // Find position to add space
110 struct allocinfo_s **pprev = &zone->info, *info;
112 info = GET_PMMVAR(*pprev);
113 if (!info || GET_PMMVAR(info->data) < start)
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 struct allocdetail_s *tempdetailp = MAKE_FLATPTR(GET_SEG(SS), &tempdetail);
125 SET_PMMVAR(*pprev, &tempdetailp->datainfo);
127 SET_PMMVAR(info->pprev, &tempdetailp->datainfo.next);
129 // Allocate final allocation info.
130 struct allocdetail_s *detail = allocSpace(
131 &ZoneTmpHigh, sizeof(*detail), MALLOC_MIN_ALIGN, NULL);
133 detail = allocSpace(&ZoneTmpLow, sizeof(*detail)
134 , MALLOC_MIN_ALIGN, NULL);
136 SET_PMMVAR(*tempdetail.datainfo.pprev, tempdetail.datainfo.next);
137 if (tempdetail.datainfo.next)
138 SET_PMMVAR(tempdetail.datainfo.next->pprev
139 , tempdetail.datainfo.pprev);
145 // Replace temp alloc space with final alloc space
146 memcpy_fl(&detail->datainfo, &tempdetailp->datainfo
147 , sizeof(detail->datainfo));
148 SET_PMMVAR(detail->handle, PMM_DEFAULT_HANDLE);
150 SET_PMMVAR(*tempdetail.datainfo.pprev, &detail->datainfo);
151 if (tempdetail.datainfo.next)
152 SET_PMMVAR(tempdetail.datainfo.next->pprev, &detail->datainfo.next);
155 // Search all zones for an allocation obtained from allocSpace()
156 static struct allocinfo_s *
157 findAlloc(void *data)
160 for (i=0; i<ARRAY_SIZE(Zones); i++) {
161 struct zone_s *zone = GET_PMMVAR(Zones[i]);
162 struct allocinfo_s *info;
163 for (info = GET_PMMVAR(zone->info); info; info = GET_PMMVAR(info->next))
164 if (GET_PMMVAR(info->data) == data)
170 // Return the last sentinal node of a zone
171 static struct allocinfo_s *
172 findLast(struct zone_s *zone)
174 struct allocinfo_s *info = GET_PMMVAR(zone->info);
178 struct allocinfo_s *next = GET_PMMVAR(info->next);
186 /****************************************************************
188 ****************************************************************/
194 dprintf(3, "malloc setup\n");
196 ZoneLow.info = ZoneHigh.info = ZoneFSeg.info = NULL;
197 ZoneTmpLow.info = ZoneTmpHigh.info = NULL;
199 // Clear memory in 0xf0000 area.
200 extern u8 code32flat_start[];
201 if ((u32)code32flat_start > BUILD_BIOS_ADDR)
202 // Clear unused parts of f-segment
203 memset((void*)BUILD_BIOS_ADDR, 0
204 , (u32)code32flat_start - BUILD_BIOS_ADDR);
205 memset(BiosTableSpace, 0, CONFIG_MAX_BIOSTABLE);
207 // Populate temp high ram
210 for (i=e820_count-1; i>=0; i--) {
211 struct e820entry *en = &e820_list[i];
212 u64 end = en->start + en->size;
215 if (en->type != E820_RAM || end > 0xffffffff)
217 u32 s = en->start, e = end;
219 u32 newe = ALIGN_DOWN(e - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
220 if (newe <= e && newe >= s) {
225 addSpace(&ZoneTmpHigh, (void*)s, (void*)e);
228 // Populate other regions
229 addSpace(&ZoneTmpLow, (void*)BUILD_STACK_ADDR, (void*)BUILD_EBDA_MINIMUM);
230 addSpace(&ZoneFSeg, BiosTableSpace, &BiosTableSpace[CONFIG_MAX_BIOSTABLE]);
231 addSpace(&ZoneLow, (void*)BUILD_LOWRAM_END, (void*)BUILD_LOWRAM_END);
233 addSpace(&ZoneHigh, (void*)highram
234 , (void*)highram + CONFIG_MAX_HIGHTABLE);
235 add_e820(highram, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
240 malloc_finalize(void)
242 dprintf(3, "malloc finalize\n");
244 // Reserve more low-mem if needed.
245 u32 endlow = GET_BDA(mem_size_kb)*1024;
246 add_e820(endlow, BUILD_LOWRAM_END-endlow, E820_RESERVED);
248 // Give back unused high ram.
249 struct allocinfo_s *info = findLast(&ZoneHigh);
251 u32 giveback = ALIGN_DOWN(info->allocend - info->dataend, PAGE_SIZE);
252 add_e820((u32)info->dataend, giveback, E820_RAM);
253 dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
256 // Clear low-memory allocations.
257 memset((void*)BUILD_STACK_ADDR, 0, BUILD_EBDA_MINIMUM - BUILD_STACK_ADDR);
261 /****************************************************************
263 ****************************************************************/
267 relocate_ebda(u32 newebda, u32 oldebda, u8 ebda_size)
269 u32 lowram = GET_BDA(mem_size_kb) * 1024;
270 if (oldebda != lowram)
271 // EBDA isn't at end of ram - give up.
274 // Do copy (this assumes memcpy copies forward - otherwise memmove
276 memcpy_fl((void*)newebda, (void*)oldebda, ebda_size * 1024);
279 dprintf(1, "ebda moved from %x to %x\n", oldebda, newebda);
280 SET_BDA(mem_size_kb, newebda / 1024);
281 SET_BDA(ebda_seg, FLATPTR_TO_SEG(newebda));
285 // Support expanding the ZoneLow dynamically.
287 zonelow_expand(u32 size, u32 align)
289 struct allocinfo_s *info = findLast(&ZoneLow);
292 u32 oldpos = (u32)GET_PMMVAR(info->allocend);
293 u32 newpos = ALIGN_DOWN(oldpos - size, align);
294 u32 bottom = (u32)GET_PMMVAR(info->dataend);
295 if (newpos >= bottom && newpos <= oldpos)
296 // Space already present.
298 u16 ebda_seg = get_ebda_seg();
299 u32 ebda_pos = (u32)MAKE_FLATPTR(ebda_seg, 0);
300 u8 ebda_size = GET_EBDA2(ebda_seg, size);
301 u32 ebda_end = ebda_pos + ebda_size * 1024;
302 if (ebda_end != bottom)
303 // Something else is after ebda - can't use any existing space.
304 newpos = ALIGN_DOWN(ebda_end - size, align);
305 u32 newbottom = ALIGN_DOWN(newpos, 1024);
306 u32 newebda = ALIGN_DOWN(newbottom - ebda_size * 1024, 1024);
307 if (newebda < BUILD_EBDA_MINIMUM)
312 int ret = relocate_ebda(newebda, ebda_pos, ebda_size);
317 if (ebda_end == bottom) {
318 SET_PMMVAR(info->data, (void*)newbottom);
319 SET_PMMVAR(info->dataend, (void*)newbottom);
321 addSpace(&ZoneLow, (void*)newbottom, (void*)ebda_end);
324 // Check if can expand the given zone to fulfill an allocation
326 allocExpandSpace(struct zone_s *zone, u32 size, u32 align
327 , struct allocinfo_s *fill)
329 void *data = allocSpace(zone, size, align, fill);
330 if (data || zone != &ZoneLow)
333 // Make sure to not move ebda while an optionrom is running.
334 if (unlikely(wait_preempt())) {
335 data = allocSpace(zone, size, align, fill);
340 zonelow_expand(size, align);
341 return allocSpace(zone, size, align, fill);
345 /****************************************************************
346 * tracked memory allocations
347 ****************************************************************/
349 // Allocate memory from the given zone and track it as a PMM allocation
351 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
356 // Find and reserve space for bookkeeping.
357 struct allocdetail_s *detail = allocSpace(
358 &ZoneTmpHigh, sizeof(*detail), MALLOC_MIN_ALIGN, NULL);
360 detail = allocSpace(&ZoneTmpLow, sizeof(*detail)
361 , MALLOC_MIN_ALIGN, NULL);
366 // Find and reserve space for main allocation
367 void *data = allocExpandSpace(zone, size, align, &detail->datainfo);
369 freeSpace(&detail->detailinfo);
373 dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
374 " ret=%p (detail=%p)\n"
375 , zone, handle, size, align
377 SET_PMMVAR(detail->handle, handle);
382 // Free a data block allocated with pmm_malloc
386 struct allocinfo_s *info = findAlloc(data);
387 if (!info || data == (void*)info || data == GET_PMMVAR(info->dataend))
389 struct allocdetail_s *detail = container_of(
390 info, struct allocdetail_s, datainfo);
391 dprintf(8, "pmm_free %p (detail=%p)\n", data, detail);
393 freeSpace(&detail->detailinfo);
397 // Find the amount of free space in a given zone.
399 pmm_getspace(struct zone_s *zone)
401 // XXX - doesn't account for ZoneLow being able to grow.
402 // XXX - results not reliable when CONFIG_THREAD_OPTIONROMS
404 struct allocinfo_s *info;
405 for (info = GET_PMMVAR(zone->info); info; info = GET_PMMVAR(info->next)) {
406 u32 space = GET_PMMVAR(info->allocend) - GET_PMMVAR(info->dataend);
407 if (space > maxspace)
411 if (zone != &ZoneTmpHigh && zone != &ZoneTmpLow)
413 // Account for space needed for PMM tracking.
414 u32 reserve = ALIGN(sizeof(struct allocdetail_s), MALLOC_MIN_ALIGN);
415 if (maxspace <= reserve)
417 return maxspace - reserve;
420 // Find the data block allocated with pmm_malloc with a given handle.
425 for (i=0; i<ARRAY_SIZE(Zones); i++) {
426 struct zone_s *zone = GET_PMMVAR(Zones[i]);
427 struct allocinfo_s *info;
428 for (info = GET_PMMVAR(zone->info); info
429 ; info = GET_PMMVAR(info->next)) {
430 if (GET_PMMVAR(info->data) != (void*)info)
432 struct allocdetail_s *detail = container_of(
433 info, struct allocdetail_s, detailinfo);
434 if (GET_PMMVAR(detail->handle) == handle)
435 return GET_PMMVAR(detail->datainfo.data);
442 /****************************************************************
444 ****************************************************************/
456 extern struct pmmheader PMMHEADER;
458 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
461 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
463 .length = sizeof(PMMHEADER),
464 .entry_seg = SEG_BIOS,
468 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
472 handle_pmm00(u16 *args)
474 u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
476 dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
477 , length, handle, flags);
478 struct zone_s *lowzone = &ZoneTmpLow, *highzone = &ZoneTmpHigh;
480 // Permanent memory request.
482 highzone = &ZoneHigh;
485 // Memory size request
491 return pmm_getspace(lowzone);
493 return pmm_getspace(highzone);
495 u32 spacelow = pmm_getspace(lowzone);
496 u32 spacehigh = pmm_getspace(highzone);
497 if (spacelow > spacehigh)
503 u32 size = length * 16;
506 u32 align = MALLOC_MIN_ALIGN;
508 align = 1<<__ffs(size);
509 if (align < MALLOC_MIN_ALIGN)
510 align = MALLOC_MIN_ALIGN;
517 return (u32)pmm_malloc(lowzone, handle, size, align);
519 return (u32)pmm_malloc(highzone, handle, size, align);
521 void *data = pmm_malloc(lowzone, handle, size, align);
524 return (u32)pmm_malloc(highzone, handle, size, align);
531 handle_pmm01(u16 *args)
533 u32 handle = *(u32*)&args[1];
534 dprintf(3, "pmm01: handle=%x\n", handle);
535 if (handle == PMM_DEFAULT_HANDLE)
537 return (u32)pmm_find(handle);
542 handle_pmm02(u16 *args)
544 u32 buffer = *(u32*)&args[1];
545 dprintf(3, "pmm02: buffer=%x\n", buffer);
546 int ret = pmm_free((void*)buffer);
554 handle_pmmXX(u16 *args)
556 return PMM_FUNCTION_NOT_SUPPORTED;
560 handle_pmm(u16 *args)
563 return PMM_FUNCTION_NOT_SUPPORTED;
566 dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
569 case 0x00: return handle_pmm00(args);
570 case 0x01: return handle_pmm01(args);
571 case 0x02: return handle_pmm02(args);
572 default: return handle_pmmXX(args);
577 extern void entry_pmm(void);
585 dprintf(3, "init PMM\n");
587 PMMHEADER.signature = PMM_SIGNATURE;
588 PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
589 PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
598 dprintf(3, "finalize PMM\n");
600 PMMHEADER.signature = 0;
601 PMMHEADER.entry_offset = 0;