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" // find_high_area
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) GET_FARVAR(0, (var))
18 #define SET_PMMVAR(var, val) SET_FARVAR(0, (var), (val))
20 #define GET_PMMVAR(var) (var)
21 #define SET_PMMVAR(var, val) do { (var) = (val); } while (0)
29 struct zone_s ZoneLow VAR32VISIBLE, ZoneHigh VAR32VISIBLE;
30 struct zone_s ZoneFSeg VAR32VISIBLE;
31 struct zone_s ZoneTmpLow VAR32VISIBLE, ZoneTmpHigh VAR32VISIBLE;
33 struct zone_s *Zones[] VAR32VISIBLE = {
34 &ZoneTmpLow, &ZoneLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
38 /****************************************************************
40 ****************************************************************/
44 relocate_ebda(u32 newebda, u32 oldebda, u8 ebda_size)
46 u32 lowram = GET_BDA(mem_size_kb) * 1024;
47 if (oldebda != lowram)
48 // EBDA isn't at end of ram - give up.
53 memcpy_far(FLATPTR_TO_SEG(newebda)
54 , (void*)FLATPTR_TO_OFFSET(newebda)
55 , FLATPTR_TO_SEG(oldebda)
56 , (void*)FLATPTR_TO_OFFSET(oldebda)
59 memmove((void*)newebda, (void*)oldebda, ebda_size * 1024);
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));
68 // Support expanding the ZoneLow dynamically.
70 zonelow_expand(u32 size, u32 align)
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.
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)
89 int ret = relocate_ebda(newebda, ebda_pos, ebda_size);
94 SET_PMMVAR(ZoneLow.bottom, newbottom);
99 /****************************************************************
101 ****************************************************************/
103 // Obtain memory from a given zone.
105 zone_malloc(struct zone_s *zone, u32 size, u32 align)
107 u32 oldpos = GET_PMMVAR(zone->cur);
108 u32 newpos = ALIGN_DOWN(oldpos - size, align);
109 if (newpos < GET_PMMVAR(zone->bottom) || newpos > oldpos)
112 SET_PMMVAR(zone->cur, newpos);
113 return (void*)newpos;
116 // Return memory to a zone (if it was the last to be allocated).
118 zone_free(struct zone_s *zone, void *data, u32 olddata)
120 if (! data || GET_PMMVAR(zone->cur) != (u32)data)
122 SET_PMMVAR(zone->cur, olddata);
125 // Find the zone that contains the given data block.
126 static struct zone_s *
127 zone_find(void *data)
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))
139 // Report the status of all the zones.
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);
158 dprintf(3, "malloc setup\n");
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;
165 // Memory under 1Meg.
166 ZoneTmpLow.bottom = BUILD_STACK_ADDR;
167 ZoneTmpLow.top = ZoneTmpLow.cur = BUILD_EBDA_MINIMUM;
169 // Permanent memory under 1Meg.
170 ZoneLow.bottom = ZoneLow.top = ZoneLow.cur = BUILD_LOWRAM_END;
172 // Find memory at the top of ram.
173 struct e820entry *e = find_high_area(CONFIG_MAX_HIGHTABLE+MALLOC_MIN_ALIGN);
175 // No memory above 1Meg
176 memset(&ZoneHigh, 0, sizeof(ZoneHigh));
177 memset(&ZoneTmpHigh, 0, sizeof(ZoneTmpHigh));
180 u32 top = e->start + e->size, bottom = e->start;
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);
188 ZoneTmpHigh.bottom = ALIGN(bottom, MALLOC_MIN_ALIGN);
189 ZoneTmpHigh.top = ZoneTmpHigh.cur = ZoneHigh.bottom;
195 dprintf(3, "malloc finalize\n");
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);
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);
208 // Clear low-memory allocations.
209 memset((void*)ZoneTmpLow.bottom, 0, ZoneTmpLow.top - ZoneTmpLow.bottom);
212 // Allocate memory from ZoneLow - growing the zone if needed.
214 zone_malloc_low(u32 size, u32 align)
216 void *ret = zone_malloc(&ZoneLow, size, align);
219 zonelow_expand(size, align);
220 return zone_malloc(&ZoneLow, size, align);
224 /****************************************************************
226 ****************************************************************/
228 // Information on PMM tracked allocations
234 struct pmmalloc_s *next;
237 struct pmmalloc_s *PMMAllocs VAR32VISIBLE;
239 // Memory zone that pmm allocation tracking info is stored in
240 #define ZONEALLOC (&ZoneTmpHigh)
242 // Allocate memory from the given zone and track it as a PMM allocation
244 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
246 u32 oldallocdata = GET_PMMVAR(ZONEALLOC->cur);
247 struct pmmalloc_s *info = zone_malloc(ZONEALLOC, sizeof(*info)
251 u32 olddata = GET_PMMVAR(zone->cur);
252 void *data = zone_malloc(zone, size, align);
253 #if 0 // XXX - gcc4.3 internal compiler error - disable for now
254 if (!data && zone == &ZoneLow) {
255 // Try to expand permanent low zone.
256 zonelow_expand(size, align);
257 olddata = GET_PMMVAR(zone->cur);
258 data = zone_malloc(zone, size, align);
262 zone_free(ZONEALLOC, info, oldallocdata);
265 dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
266 " ret=%p (info=%p)\n"
267 , zone, handle, size, align
269 SET_PMMVAR(info->data, data);
270 SET_PMMVAR(info->olddata, olddata);
271 SET_PMMVAR(info->handle, handle);
272 SET_PMMVAR(info->oldallocdata, oldallocdata);
273 SET_PMMVAR(info->next, GET_PMMVAR(PMMAllocs));
274 SET_PMMVAR(PMMAllocs, info);
278 // Free a raw data block (either from a zone or from pmm alloc list).
280 pmm_free_data(struct zone_s *zone, void *data, u32 olddata)
282 if (GET_PMMVAR(zone->cur) == (u32)data) {
283 zone_free(zone, data, olddata);
286 struct pmmalloc_s *info;
287 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
288 if (GET_PMMVAR(info->olddata) == (u32)data) {
289 SET_PMMVAR(info->olddata, olddata);
291 } else if (GET_PMMVAR(info->oldallocdata) == (u32)data) {
292 SET_PMMVAR(info->oldallocdata, olddata);
297 // Free a data block allocated with pmm_malloc
301 struct zone_s *zone = zone_find(GET_PMMVAR(data));
304 struct pmmalloc_s **pinfo = &PMMAllocs;
306 struct pmmalloc_s *info = GET_PMMVAR(*pinfo);
309 if (GET_PMMVAR(info->data) == data) {
310 SET_PMMVAR(*pinfo, GET_PMMVAR(info->next));
311 u32 oldallocdata = GET_PMMVAR(info->oldallocdata);
312 u32 olddata = GET_PMMVAR(info->olddata);
313 pmm_free_data(zone, data, olddata);
314 pmm_free_data(ZONEALLOC, info, oldallocdata);
315 dprintf(8, "pmm_free data=%p zone=%p olddata=%p oldallocdata=%p"
317 , data, zone, (void*)olddata, (void*)oldallocdata
325 // Find the amount of free space in a given zone.
327 pmm_getspace(struct zone_s *zone)
329 // XXX - doesn't account for ZoneLow being able to grow.
330 u32 space = GET_PMMVAR(zone->cur) - GET_PMMVAR(zone->bottom);
331 if (zone != ZONEALLOC)
333 u32 reserve = ALIGN(sizeof(struct pmmalloc_s), MALLOC_MIN_ALIGN);
334 if (space <= reserve)
336 return space - reserve;
339 // Find the data block allocated with pmm_malloc with a given handle.
343 struct pmmalloc_s *info;
344 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
345 if (GET_PMMVAR(info->handle) == handle)
346 return GET_PMMVAR(info->data);
351 /****************************************************************
353 ****************************************************************/
365 extern struct pmmheader PMMHEADER;
367 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
370 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
372 .length = sizeof(PMMHEADER),
373 .entry_seg = SEG_BIOS,
377 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
381 handle_pmm00(u16 *args)
383 u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
385 dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
386 , length, handle, flags);
387 struct zone_s *lowzone = &ZoneTmpLow, *highzone = &ZoneTmpHigh;
389 // Permanent memory request.
391 highzone = &ZoneHigh;
394 // Memory size request
400 return pmm_getspace(lowzone);
402 return pmm_getspace(highzone);
404 u32 spacelow = pmm_getspace(lowzone);
405 u32 spacehigh = pmm_getspace(highzone);
406 if (spacelow > spacehigh)
412 u32 size = length * 16;
415 u32 align = MALLOC_MIN_ALIGN;
417 align = 1<<__ffs(size);
418 if (align < MALLOC_MIN_ALIGN)
419 align = MALLOC_MIN_ALIGN;
426 return (u32)pmm_malloc(lowzone, handle, size, align);
428 return (u32)pmm_malloc(highzone, handle, size, align);
430 void *data = pmm_malloc(lowzone, handle, size, align);
433 return (u32)pmm_malloc(highzone, handle, size, align);
440 handle_pmm01(u16 *args)
442 u32 handle = *(u32*)&args[1];
443 dprintf(3, "pmm01: handle=%x\n", handle);
444 if (handle == 0xFFFFFFFF)
446 return (u32)pmm_find(handle);
451 handle_pmm02(u16 *args)
453 u32 buffer = *(u32*)&args[1];
454 dprintf(3, "pmm02: buffer=%x\n", buffer);
455 int ret = pmm_free((void*)buffer);
463 handle_pmmXX(u16 *args)
465 return PMM_FUNCTION_NOT_SUPPORTED;
469 handle_pmm(u16 *args)
472 return PMM_FUNCTION_NOT_SUPPORTED;
475 dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
478 case 0x00: return handle_pmm00(args);
479 case 0x01: return handle_pmm01(args);
480 case 0x02: return handle_pmm02(args);
481 default: return handle_pmmXX(args);
486 extern void entry_pmm();
494 dprintf(3, "init PMM\n");
498 PMMHEADER.signature = PMM_SIGNATURE;
499 PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
500 PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
509 dprintf(3, "finalize PMM\n");
511 PMMHEADER.signature = 0;
512 PMMHEADER.entry_offset = 0;