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" // e820_list
10 #include "farptr.h" // GET_FARVAR
11 #include "biosvar.h" // EBDA_SEGMENT_MINIMUM
14 /****************************************************************
16 ****************************************************************/
19 // The 16bit pmm entry points runs in "big real" mode, and can
20 // therefore read/write to the 32bit malloc variables.
21 #define GET_PMMVAR(var) GET_FARVAR(0, (var))
22 #define SET_PMMVAR(var, val) SET_FARVAR(0, (var), (val))
24 #define GET_PMMVAR(var) (var)
25 #define SET_PMMVAR(var, val) do { (var) = (val); } while (0)
33 struct zone_s ZoneHigh VAR32VISIBLE, ZoneFSeg VAR32VISIBLE;
34 struct zone_s ZoneTmpLow VAR32VISIBLE, ZoneTmpHigh VAR32VISIBLE;
36 struct zone_s *Zones[] VAR32VISIBLE = {
37 &ZoneTmpLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
40 // Obtain memory from a given zone.
42 zone_malloc(struct zone_s *zone, u32 size, u32 align)
44 u32 newpos = (GET_PMMVAR(zone->cur) - size) / align * align;
45 if ((s32)(newpos - GET_PMMVAR(zone->bottom)) < 0)
48 SET_PMMVAR(zone->cur, newpos);
52 // Return memory to a zone (if it was the last to be allocated).
54 zone_free(struct zone_s *zone, void *data, u32 olddata)
56 if (! data || GET_PMMVAR(zone->cur) != (u32)data)
58 SET_PMMVAR(zone->cur, olddata);
61 // Find the zone that contains the given data block.
62 static struct zone_s *
66 for (i=0; i<ARRAY_SIZE(Zones); i++) {
67 struct zone_s *zone = GET_PMMVAR(Zones[i]);
68 if ((u32)data >= GET_PMMVAR(zone->cur)
69 && (u32)data < GET_PMMVAR(zone->top))
75 // Report the status of all the zones.
80 for (i=0; i<ARRAY_SIZE(Zones); i++) {
81 struct zone_s *zone = Zones[i];
82 u32 used = zone->top - zone->cur;
83 u32 avail = zone->top - zone->bottom;
84 u32 pct = avail ? ((100 * used) / avail) : 0;
85 dprintf(2, "zone %d: %08x-%08x used=%d (%d%%)\n"
86 , i, zone->bottom, zone->top, used, pct);
90 // Allocate memory at the top of 32bit ram.
94 return zone_malloc(&ZoneHigh, size, MALLOC_MIN_ALIGN);
97 // Allocate memory in the 0xf0000-0x100000 area of ram.
101 return zone_malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
108 dprintf(3, "malloc setup\n");
110 // Memory in 0xf0000 area.
111 memset(BiosTableSpace, 0, CONFIG_MAX_BIOSTABLE);
112 ZoneFSeg.bottom = (u32)BiosTableSpace;
113 ZoneFSeg.top = ZoneFSeg.cur = ZoneFSeg.bottom + CONFIG_MAX_BIOSTABLE;
115 // Memory under 1Meg.
116 ZoneTmpLow.bottom = BUILD_STACK_ADDR;
117 ZoneTmpLow.top = ZoneTmpLow.cur = (u32)MAKE_FLATPTR(EBDA_SEGMENT_MINIMUM, 0);
119 // Find memory at the top of ram.
120 u32 top = 0, bottom = 0;
122 for (i=e820_count-1; i>=0; i--) {
123 struct e820entry *e = &e820_list[i];
124 u64 end = e->start + e->size;
125 if (e->type != E820_RAM || end > 0xffffffff
126 || e->size < CONFIG_MAX_HIGHTABLE + MALLOC_MIN_ALIGN)
132 if (top < 1024*1024 + CONFIG_MAX_HIGHTABLE) {
133 // No memory above 1Meg
134 memset(&ZoneHigh, 0, sizeof(ZoneHigh));
135 memset(&ZoneTmpHigh, 0, sizeof(ZoneHigh));
139 // Memory at top of ram.
140 ZoneHigh.bottom = ALIGN(top - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
141 ZoneHigh.top = ZoneHigh.cur = ZoneHigh.bottom + CONFIG_MAX_HIGHTABLE;
142 add_e820(ZoneHigh.bottom, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
145 ZoneTmpHigh.bottom = ALIGN(bottom, MALLOC_MIN_ALIGN);
146 ZoneTmpHigh.top = ZoneTmpHigh.cur = ZoneHigh.bottom;
152 dprintf(3, "malloc finalize\n");
156 // Give back unused high ram.
157 u32 giveback = (ZoneHigh.cur - ZoneHigh.bottom) / 4096 * 4096;
158 add_e820(ZoneHigh.bottom, giveback, E820_RAM);
159 dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
161 // Clear low-memory allocations.
162 memset((void*)ZoneTmpLow.bottom, 0, ZoneTmpLow.top - ZoneTmpLow.bottom);
166 /****************************************************************
168 ****************************************************************/
170 // Information on PMM tracked allocations
176 struct pmmalloc_s *next;
179 struct pmmalloc_s *PMMAllocs VAR32VISIBLE;
181 // Memory zone that pmm allocation tracking info is stored in
182 #define ZONEALLOC (&ZoneTmpHigh)
184 // Allocate memory from the given zone and track it as a PMM allocation
186 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
188 u32 oldallocdata = GET_PMMVAR(ZONEALLOC->cur);
189 struct pmmalloc_s *info = zone_malloc(ZONEALLOC, sizeof(*info)
193 u32 olddata = GET_PMMVAR(zone->cur);
194 void *data = zone_malloc(zone, size, align);
196 zone_free(ZONEALLOC, info, oldallocdata);
199 dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
200 " ret=%p (info=%p)\n"
201 , zone, handle, size, align
203 SET_PMMVAR(info->data, data);
204 SET_PMMVAR(info->olddata, olddata);
205 SET_PMMVAR(info->handle, handle);
206 SET_PMMVAR(info->oldallocdata, oldallocdata);
207 SET_PMMVAR(info->next, GET_PMMVAR(PMMAllocs));
208 SET_PMMVAR(PMMAllocs, info);
212 // Free a raw data block (either from a zone or from pmm alloc list).
214 pmm_free_data(struct zone_s *zone, void *data, u32 olddata)
216 if (GET_PMMVAR(zone->cur) == (u32)data) {
217 zone_free(zone, data, olddata);
220 struct pmmalloc_s *info;
221 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
222 if (GET_PMMVAR(info->olddata) == (u32)data) {
223 SET_PMMVAR(info->olddata, olddata);
225 } else if (GET_PMMVAR(info->oldallocdata) == (u32)data) {
226 SET_PMMVAR(info->oldallocdata, olddata);
231 // Free a data block allocated with pmm_malloc
235 struct zone_s *zone = zone_find(GET_PMMVAR(data));
238 struct pmmalloc_s **pinfo = &PMMAllocs;
240 struct pmmalloc_s *info = GET_PMMVAR(*pinfo);
243 if (GET_PMMVAR(info->data) == data) {
244 SET_PMMVAR(*pinfo, GET_PMMVAR(info->next));
245 u32 oldallocdata = GET_PMMVAR(info->oldallocdata);
246 u32 olddata = GET_PMMVAR(info->olddata);
247 pmm_free_data(zone, data, olddata);
248 pmm_free_data(ZONEALLOC, info, oldallocdata);
249 dprintf(8, "pmm_free data=%p zone=%p olddata=%p oldallocdata=%p"
251 , data, zone, (void*)olddata, (void*)oldallocdata
259 // Find the amount of free space in a given zone.
261 pmm_getspace(struct zone_s *zone)
263 u32 space = GET_PMMVAR(zone->cur) - GET_PMMVAR(zone->bottom);
264 if (zone != ZONEALLOC)
266 u32 reserve = ALIGN(sizeof(struct pmmalloc_s), MALLOC_MIN_ALIGN);
267 if (space <= reserve)
269 return space - reserve;
272 // Find the data block allocated with pmm_malloc with a given handle.
276 struct pmmalloc_s *info;
277 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
278 if (GET_PMMVAR(info->handle) == handle)
279 return GET_PMMVAR(info->data);
284 /****************************************************************
286 ****************************************************************/
298 extern struct pmmheader PMMHEADER;
300 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
303 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
305 .length = sizeof(PMMHEADER),
306 .entry_seg = SEG_BIOS,
310 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
314 handle_pmm00(u16 *args)
316 u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
318 dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
319 , length, handle, flags);
321 // Memory size request
327 return pmm_getspace(&ZoneTmpLow);
329 return pmm_getspace(&ZoneTmpHigh);
331 u32 spacelow = pmm_getspace(&ZoneTmpLow);
332 u32 spacehigh = pmm_getspace(&ZoneTmpHigh);
333 if (spacelow > spacehigh)
339 u32 size = length * 16;
342 u32 align = MALLOC_MIN_ALIGN;
344 align = 1<<__ffs(size);
345 if (align < MALLOC_MIN_ALIGN)
346 align = MALLOC_MIN_ALIGN;
353 return (u32)pmm_malloc(&ZoneTmpLow, handle, size, align);
355 return (u32)pmm_malloc(&ZoneTmpHigh, handle, size, align);
357 void *data = pmm_malloc(&ZoneTmpLow, handle, size, align);
360 return (u32)pmm_malloc(&ZoneTmpHigh, handle, size, align);
367 handle_pmm01(u16 *args)
369 u32 handle = *(u32*)&args[1];
370 dprintf(3, "pmm01: handle=%x\n", handle);
371 if (handle == 0xFFFFFFFF)
373 return (u32)pmm_find(handle);
378 handle_pmm02(u16 *args)
380 u32 buffer = *(u32*)&args[1];
381 dprintf(3, "pmm02: buffer=%x\n", buffer);
382 int ret = pmm_free((void*)buffer);
390 handle_pmmXX(u16 *args)
392 return PMM_FUNCTION_NOT_SUPPORTED;
396 handle_pmm(u16 *args)
399 return PMM_FUNCTION_NOT_SUPPORTED;
402 dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
405 case 0x00: return handle_pmm00(args);
406 case 0x01: return handle_pmm01(args);
407 case 0x02: return handle_pmm02(args);
408 default: return handle_pmmXX(args);
413 extern void entry_pmm();
421 dprintf(3, "init PMM\n");
425 PMMHEADER.signature = PMM_SIGNATURE;
426 PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
427 PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
436 dprintf(3, "finalize PMM\n");
438 PMMHEADER.signature = 0;
439 PMMHEADER.entry_offset = 0;