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" // 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 ZoneLow VAR32VISIBLE, ZoneHigh VAR32VISIBLE;
34 struct zone_s ZoneFSeg VAR32VISIBLE;
35 struct zone_s ZoneTmpLow VAR32VISIBLE, ZoneTmpHigh VAR32VISIBLE;
37 struct zone_s *Zones[] VAR32VISIBLE = {
38 &ZoneTmpLow, &ZoneLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
41 // Obtain memory from a given zone.
43 zone_malloc(struct zone_s *zone, u32 size, u32 align)
45 u32 oldpos = GET_PMMVAR(zone->cur);
46 u32 newpos = ALIGN_DOWN(oldpos - size, align);
47 if (newpos < GET_PMMVAR(zone->bottom) || newpos > oldpos)
50 SET_PMMVAR(zone->cur, newpos);
54 // Return memory to a zone (if it was the last to be allocated).
56 zone_free(struct zone_s *zone, void *data, u32 olddata)
58 if (! data || GET_PMMVAR(zone->cur) != (u32)data)
60 SET_PMMVAR(zone->cur, olddata);
63 // Find the zone that contains the given data block.
64 static struct zone_s *
68 for (i=0; i<ARRAY_SIZE(Zones); i++) {
69 struct zone_s *zone = GET_PMMVAR(Zones[i]);
70 if ((u32)data >= GET_PMMVAR(zone->cur)
71 && (u32)data < GET_PMMVAR(zone->top))
77 // Report the status of all the zones.
82 for (i=0; i<ARRAY_SIZE(Zones); i++) {
83 struct zone_s *zone = Zones[i];
84 u32 used = zone->top - zone->cur;
85 u32 avail = zone->top - zone->bottom;
86 u32 pct = avail ? ((100 * used) / avail) : 0;
87 dprintf(2, "zone %d: %08x-%08x used=%d (%d%%)\n"
88 , i, zone->bottom, zone->top, used, pct);
96 dprintf(3, "malloc setup\n");
98 // Memory in 0xf0000 area.
99 memset(BiosTableSpace, 0, CONFIG_MAX_BIOSTABLE);
100 ZoneFSeg.bottom = (u32)BiosTableSpace;
101 ZoneFSeg.top = ZoneFSeg.cur = ZoneFSeg.bottom + CONFIG_MAX_BIOSTABLE;
103 // Memory under 1Meg.
104 ZoneTmpLow.bottom = BUILD_STACK_ADDR;
105 ZoneTmpLow.top = ZoneTmpLow.cur = (u32)MAKE_FLATPTR(EBDA_SEGMENT_MINIMUM, 0);
107 // Permanent memory under 1Meg. XXX - not implemented yet.
108 ZoneLow.bottom = ZoneLow.top = ZoneLow.cur = 0xa0000;
110 // Find memory at the top of ram.
111 struct e820entry *e = find_high_area(CONFIG_MAX_HIGHTABLE+MALLOC_MIN_ALIGN);
113 // No memory above 1Meg
114 memset(&ZoneHigh, 0, sizeof(ZoneHigh));
115 memset(&ZoneTmpHigh, 0, sizeof(ZoneTmpHigh));
118 u32 top = e->start + e->size, bottom = e->start;
120 // Memory at top of ram.
121 ZoneHigh.bottom = ALIGN(top - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
122 ZoneHigh.top = ZoneHigh.cur = ZoneHigh.bottom + CONFIG_MAX_HIGHTABLE;
123 add_e820(ZoneHigh.bottom, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
126 ZoneTmpHigh.bottom = ALIGN(bottom, MALLOC_MIN_ALIGN);
127 ZoneTmpHigh.top = ZoneTmpHigh.cur = ZoneHigh.bottom;
133 dprintf(3, "malloc finalize\n");
137 // Give back unused high ram.
138 u32 giveback = ALIGN_DOWN(ZoneHigh.cur - ZoneHigh.bottom, PAGE_SIZE);
139 add_e820(ZoneHigh.bottom, giveback, E820_RAM);
140 dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
142 // Clear low-memory allocations.
143 memset((void*)ZoneTmpLow.bottom, 0, ZoneTmpLow.top - ZoneTmpLow.bottom);
147 /****************************************************************
149 ****************************************************************/
151 // Information on PMM tracked allocations
157 struct pmmalloc_s *next;
160 struct pmmalloc_s *PMMAllocs VAR32VISIBLE;
162 // Memory zone that pmm allocation tracking info is stored in
163 #define ZONEALLOC (&ZoneTmpHigh)
165 // Allocate memory from the given zone and track it as a PMM allocation
167 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
169 u32 oldallocdata = GET_PMMVAR(ZONEALLOC->cur);
170 struct pmmalloc_s *info = zone_malloc(ZONEALLOC, sizeof(*info)
174 u32 olddata = GET_PMMVAR(zone->cur);
175 void *data = zone_malloc(zone, size, align);
177 zone_free(ZONEALLOC, info, oldallocdata);
180 dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
181 " ret=%p (info=%p)\n"
182 , zone, handle, size, align
184 SET_PMMVAR(info->data, data);
185 SET_PMMVAR(info->olddata, olddata);
186 SET_PMMVAR(info->handle, handle);
187 SET_PMMVAR(info->oldallocdata, oldallocdata);
188 SET_PMMVAR(info->next, GET_PMMVAR(PMMAllocs));
189 SET_PMMVAR(PMMAllocs, info);
193 // Free a raw data block (either from a zone or from pmm alloc list).
195 pmm_free_data(struct zone_s *zone, void *data, u32 olddata)
197 if (GET_PMMVAR(zone->cur) == (u32)data) {
198 zone_free(zone, data, olddata);
201 struct pmmalloc_s *info;
202 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
203 if (GET_PMMVAR(info->olddata) == (u32)data) {
204 SET_PMMVAR(info->olddata, olddata);
206 } else if (GET_PMMVAR(info->oldallocdata) == (u32)data) {
207 SET_PMMVAR(info->oldallocdata, olddata);
212 // Free a data block allocated with pmm_malloc
216 struct zone_s *zone = zone_find(GET_PMMVAR(data));
219 struct pmmalloc_s **pinfo = &PMMAllocs;
221 struct pmmalloc_s *info = GET_PMMVAR(*pinfo);
224 if (GET_PMMVAR(info->data) == data) {
225 SET_PMMVAR(*pinfo, GET_PMMVAR(info->next));
226 u32 oldallocdata = GET_PMMVAR(info->oldallocdata);
227 u32 olddata = GET_PMMVAR(info->olddata);
228 pmm_free_data(zone, data, olddata);
229 pmm_free_data(ZONEALLOC, info, oldallocdata);
230 dprintf(8, "pmm_free data=%p zone=%p olddata=%p oldallocdata=%p"
232 , data, zone, (void*)olddata, (void*)oldallocdata
240 // Find the amount of free space in a given zone.
242 pmm_getspace(struct zone_s *zone)
244 u32 space = GET_PMMVAR(zone->cur) - GET_PMMVAR(zone->bottom);
245 if (zone != ZONEALLOC)
247 u32 reserve = ALIGN(sizeof(struct pmmalloc_s), MALLOC_MIN_ALIGN);
248 if (space <= reserve)
250 return space - reserve;
253 // Find the data block allocated with pmm_malloc with a given handle.
257 struct pmmalloc_s *info;
258 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
259 if (GET_PMMVAR(info->handle) == handle)
260 return GET_PMMVAR(info->data);
265 /****************************************************************
267 ****************************************************************/
279 extern struct pmmheader PMMHEADER;
281 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
284 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
286 .length = sizeof(PMMHEADER),
287 .entry_seg = SEG_BIOS,
291 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
295 handle_pmm00(u16 *args)
297 u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
299 dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
300 , length, handle, flags);
301 struct zone_s *lowzone = &ZoneTmpLow, *highzone = &ZoneTmpHigh;
303 // Permanent memory request.
305 highzone = &ZoneHigh;
308 // Memory size request
314 return pmm_getspace(lowzone);
316 return pmm_getspace(highzone);
318 u32 spacelow = pmm_getspace(lowzone);
319 u32 spacehigh = pmm_getspace(highzone);
320 if (spacelow > spacehigh)
326 u32 size = length * 16;
329 u32 align = MALLOC_MIN_ALIGN;
331 align = 1<<__ffs(size);
332 if (align < MALLOC_MIN_ALIGN)
333 align = MALLOC_MIN_ALIGN;
340 return (u32)pmm_malloc(lowzone, handle, size, align);
342 return (u32)pmm_malloc(highzone, handle, size, align);
344 void *data = pmm_malloc(lowzone, handle, size, align);
347 return (u32)pmm_malloc(highzone, handle, size, align);
354 handle_pmm01(u16 *args)
356 u32 handle = *(u32*)&args[1];
357 dprintf(3, "pmm01: handle=%x\n", handle);
358 if (handle == 0xFFFFFFFF)
360 return (u32)pmm_find(handle);
365 handle_pmm02(u16 *args)
367 u32 buffer = *(u32*)&args[1];
368 dprintf(3, "pmm02: buffer=%x\n", buffer);
369 int ret = pmm_free((void*)buffer);
377 handle_pmmXX(u16 *args)
379 return PMM_FUNCTION_NOT_SUPPORTED;
383 handle_pmm(u16 *args)
386 return PMM_FUNCTION_NOT_SUPPORTED;
389 dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
392 case 0x00: return handle_pmm00(args);
393 case 0x01: return handle_pmm01(args);
394 case 0x02: return handle_pmm02(args);
395 default: return handle_pmmXX(args);
400 extern void entry_pmm();
408 dprintf(3, "init PMM\n");
412 PMMHEADER.signature = PMM_SIGNATURE;
413 PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
414 PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
423 dprintf(3, "finalize PMM\n");
425 PMMHEADER.signature = 0;
426 PMMHEADER.entry_offset = 0;