blob: b3eef4bc314a725661f0094651c20dadd4c43321 [file] [log] [blame]
Kevin O'Connore54ee382009-07-26 19:33:13 -04001// Post memory manager (PMM) calls
2//
3// Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
4//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6
7#include "util.h" // checksum
8#include "config.h" // BUILD_BIOS_ADDR
Kevin O'Connor0bf92702009-08-01 11:45:37 -04009#include "memmap.h" // e820_list
10#include "farptr.h" // GET_FARVAR
11#include "biosvar.h" // EBDA_SEGMENT_MINIMUM
12
13
14/****************************************************************
15 * malloc
16 ****************************************************************/
17
18#if MODE16
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))
23#else
24#define GET_PMMVAR(var) (var)
25#define SET_PMMVAR(var, val) do { (var) = (val); } while (0)
26#endif
27
28// Zone definitions
29struct zone_s {
30 u32 top, bottom, cur;
31};
32
33struct zone_s ZoneHigh VAR32VISIBLE, ZoneFSeg VAR32VISIBLE;
34struct zone_s ZoneTmpLow VAR32VISIBLE, ZoneTmpHigh VAR32VISIBLE;
35
36struct zone_s *Zones[] VAR32VISIBLE = {
37 &ZoneTmpLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
38};
39
40// Obtain memory from a given zone.
41static void *
42zone_malloc(struct zone_s *zone, u32 size, u32 align)
43{
44 u32 newpos = (GET_PMMVAR(zone->cur) - size) / align * align;
45 if ((s32)(newpos - GET_PMMVAR(zone->bottom)) < 0)
46 // No space
47 return NULL;
48 SET_PMMVAR(zone->cur, newpos);
49 return (void*)newpos;
50}
51
52// Return memory to a zone (if it was the last to be allocated).
53static void
54zone_free(struct zone_s *zone, void *data, u32 olddata)
55{
56 if (! data || GET_PMMVAR(zone->cur) != (u32)data)
57 return;
58 SET_PMMVAR(zone->cur, olddata);
59}
60
61// Find the zone that contains the given data block.
62static struct zone_s *
63zone_find(void *data)
64{
65 int i;
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))
70 return zone;
71 }
72 return NULL;
73}
74
75// Report the status of all the zones.
76static void
77dumpZones()
78{
79 int i;
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;
Kevin O'Connor9c3e7472009-08-02 12:33:58 -040084 u32 pct = avail ? ((100 * used) / avail) : 0;
Kevin O'Connor0bf92702009-08-01 11:45:37 -040085 dprintf(2, "zone %d: %08x-%08x used=%d (%d%%)\n"
Kevin O'Connor9c3e7472009-08-02 12:33:58 -040086 , i, zone->bottom, zone->top, used, pct);
Kevin O'Connor0bf92702009-08-01 11:45:37 -040087 }
88}
89
90// Allocate memory at the top of 32bit ram.
91void *
92malloc_high(u32 size)
93{
94 return zone_malloc(&ZoneHigh, size, MALLOC_MIN_ALIGN);
95}
96
97// Allocate memory in the 0xf0000-0x100000 area of ram.
98void *
99malloc_fseg(u32 size)
100{
101 return zone_malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
102}
103
104void
105malloc_setup()
106{
107 ASSERT32();
108 dprintf(3, "malloc setup\n");
109
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;
114
115 // Memory under 1Meg.
116 ZoneTmpLow.bottom = BUILD_STACK_ADDR;
117 ZoneTmpLow.top = ZoneTmpLow.cur = (u32)MAKE_FLATPTR(EBDA_SEGMENT_MINIMUM, 0);
118
119 // Find memory at the top of ram.
120 u32 top = 0, bottom = 0;
121 int i;
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
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400126 || e->size < CONFIG_MAX_HIGHTABLE + MALLOC_MIN_ALIGN)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400127 continue;
128 top = end;
129 bottom = e->start;
130 break;
131 }
132 if (top < 1024*1024 + CONFIG_MAX_HIGHTABLE) {
133 // No memory above 1Meg
134 memset(&ZoneHigh, 0, sizeof(ZoneHigh));
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400135 memset(&ZoneTmpHigh, 0, sizeof(ZoneHigh));
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400136 return;
137 }
138
139 // Memory at top of ram.
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400140 ZoneHigh.bottom = ALIGN(top - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400141 ZoneHigh.top = ZoneHigh.cur = ZoneHigh.bottom + CONFIG_MAX_HIGHTABLE;
142 add_e820(ZoneHigh.bottom, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
143
144 // Memory above 1Meg
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400145 ZoneTmpHigh.bottom = ALIGN(bottom, MALLOC_MIN_ALIGN);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400146 ZoneTmpHigh.top = ZoneTmpHigh.cur = ZoneHigh.bottom;
147}
148
149void
150malloc_finalize()
151{
152 dprintf(3, "malloc finalize\n");
153
154 dumpZones();
155
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);
160
161 // Clear low-memory allocations.
162 memset((void*)ZoneTmpLow.bottom, 0, ZoneTmpLow.top - ZoneTmpLow.bottom);
163}
164
165
166/****************************************************************
167 * pmm allocation
168 ****************************************************************/
169
170// Information on PMM tracked allocations
171struct pmmalloc_s {
172 void *data;
173 u32 olddata;
174 u32 handle;
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400175 u32 oldallocdata;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400176 struct pmmalloc_s *next;
177};
178
179struct pmmalloc_s *PMMAllocs VAR32VISIBLE;
180
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400181// Memory zone that pmm allocation tracking info is stored in
182#define ZONEALLOC (&ZoneTmpHigh)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400183
184// Allocate memory from the given zone and track it as a PMM allocation
185static void *
186pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
187{
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400188 u32 oldallocdata = GET_PMMVAR(ZONEALLOC->cur);
189 struct pmmalloc_s *info = zone_malloc(ZONEALLOC, sizeof(*info)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400190 , MALLOC_MIN_ALIGN);
191 if (!info)
192 return NULL;
193 u32 olddata = GET_PMMVAR(zone->cur);
194 void *data = zone_malloc(zone, size, align);
195 if (! data) {
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400196 zone_free(ZONEALLOC, info, oldallocdata);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400197 return NULL;
198 }
199 dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
200 " ret=%p (info=%p)\n"
201 , zone, handle, size, align
202 , data, info);
203 SET_PMMVAR(info->data, data);
204 SET_PMMVAR(info->olddata, olddata);
205 SET_PMMVAR(info->handle, handle);
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400206 SET_PMMVAR(info->oldallocdata, oldallocdata);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400207 SET_PMMVAR(info->next, GET_PMMVAR(PMMAllocs));
208 SET_PMMVAR(PMMAllocs, info);
209 return data;
210}
211
212// Free a raw data block (either from a zone or from pmm alloc list).
213static void
214pmm_free_data(struct zone_s *zone, void *data, u32 olddata)
215{
216 if (GET_PMMVAR(zone->cur) == (u32)data) {
217 zone_free(zone, data, olddata);
218 return;
219 }
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);
224 return;
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400225 } else if (GET_PMMVAR(info->oldallocdata) == (u32)data) {
226 SET_PMMVAR(info->oldallocdata, olddata);
227 return;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400228 }
229}
230
231// Free a data block allocated with pmm_malloc
232static int
233pmm_free(void *data)
234{
235 struct zone_s *zone = zone_find(GET_PMMVAR(data));
236 if (!zone)
237 return -1;
238 struct pmmalloc_s **pinfo = &PMMAllocs;
239 for (;;) {
240 struct pmmalloc_s *info = GET_PMMVAR(*pinfo);
241 if (!info)
242 return -1;
243 if (GET_PMMVAR(info->data) == data) {
244 SET_PMMVAR(*pinfo, GET_PMMVAR(info->next));
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400245 u32 oldallocdata = GET_PMMVAR(info->oldallocdata);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400246 u32 olddata = GET_PMMVAR(info->olddata);
247 pmm_free_data(zone, data, olddata);
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400248 pmm_free_data(ZONEALLOC, info, oldallocdata);
249 dprintf(8, "pmm_free data=%p zone=%p olddata=%p oldallocdata=%p"
250 " info=%p\n"
251 , data, zone, (void*)olddata, (void*)oldallocdata
252 , info);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400253 return 0;
254 }
255 pinfo = &info->next;
256 }
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400257}
258
259// Find the amount of free space in a given zone.
260static u32
261pmm_getspace(struct zone_s *zone)
262{
263 u32 space = GET_PMMVAR(zone->cur) - GET_PMMVAR(zone->bottom);
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400264 if (zone != ZONEALLOC)
265 return space;
266 u32 reserve = ALIGN(sizeof(struct pmmalloc_s), MALLOC_MIN_ALIGN);
267 if (space <= reserve)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400268 return 0;
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400269 return space - reserve;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400270}
271
272// Find the data block allocated with pmm_malloc with a given handle.
273static void *
274pmm_find(u32 handle)
275{
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);
280 return NULL;
281}
282
283
284/****************************************************************
285 * pmm interface
286 ****************************************************************/
Kevin O'Connore54ee382009-07-26 19:33:13 -0400287
288struct pmmheader {
289 u32 signature;
290 u8 version;
291 u8 length;
292 u8 checksum;
293 u16 entry_offset;
294 u16 entry_seg;
295 u8 reserved[5];
296} PACKED;
297
298extern struct pmmheader PMMHEADER;
299
300#define PMM_SIGNATURE 0x4d4d5024 // $PMM
301
302#if CONFIG_PMM
303struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
304 .version = 0x01,
305 .length = sizeof(PMMHEADER),
306 .entry_seg = SEG_BIOS,
307};
308#endif
309
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400310#define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
Kevin O'Connore54ee382009-07-26 19:33:13 -0400311
312// PMM - allocate
313static u32
314handle_pmm00(u16 *args)
315{
316 u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
317 u16 flags = args[5];
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400318 dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
Kevin O'Connore54ee382009-07-26 19:33:13 -0400319 , length, handle, flags);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400320 if (!length) {
321 // Memory size request
322 switch (flags & 3) {
323 default:
324 case 0:
325 return 0;
326 case 1:
327 return pmm_getspace(&ZoneTmpLow);
328 case 2:
329 return pmm_getspace(&ZoneTmpHigh);
330 case 3: {
331 u32 spacelow = pmm_getspace(&ZoneTmpLow);
332 u32 spacehigh = pmm_getspace(&ZoneTmpHigh);
333 if (spacelow > spacehigh)
334 return spacelow;
335 return spacehigh;
336 }
337 }
338 }
339 u32 size = length * 16;
340 if ((s32)size <= 0)
341 return 0;
342 u32 align = MALLOC_MIN_ALIGN;
343 if (flags & 4) {
344 align = 1<<__ffs(size);
345 if (align < MALLOC_MIN_ALIGN)
346 align = MALLOC_MIN_ALIGN;
347 }
348 switch (flags & 3) {
349 default:
350 case 0:
351 return 0;
352 case 1:
353 return (u32)pmm_malloc(&ZoneTmpLow, handle, size, align);
354 case 2:
355 return (u32)pmm_malloc(&ZoneTmpHigh, handle, size, align);
356 case 3: {
357 void *data = pmm_malloc(&ZoneTmpLow, handle, size, align);
358 if (data)
359 return (u32)data;
360 return (u32)pmm_malloc(&ZoneTmpHigh, handle, size, align);
361 }
362 }
Kevin O'Connore54ee382009-07-26 19:33:13 -0400363}
364
365// PMM - find
366static u32
367handle_pmm01(u16 *args)
368{
369 u32 handle = *(u32*)&args[1];
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400370 dprintf(3, "pmm01: handle=%x\n", handle);
371 if (handle == 0xFFFFFFFF)
372 return 0;
373 return (u32)pmm_find(handle);
Kevin O'Connore54ee382009-07-26 19:33:13 -0400374}
375
376// PMM - deallocate
377static u32
378handle_pmm02(u16 *args)
379{
380 u32 buffer = *(u32*)&args[1];
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400381 dprintf(3, "pmm02: buffer=%x\n", buffer);
382 int ret = pmm_free((void*)buffer);
383 if (ret)
384 // Error
385 return 1;
Kevin O'Connore54ee382009-07-26 19:33:13 -0400386 return 0;
387}
388
389static u32
390handle_pmmXX(u16 *args)
391{
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400392 return PMM_FUNCTION_NOT_SUPPORTED;
Kevin O'Connore54ee382009-07-26 19:33:13 -0400393}
394
395u32 VISIBLE16
396handle_pmm(u16 *args)
397{
398 if (! CONFIG_PMM)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400399 return PMM_FUNCTION_NOT_SUPPORTED;
Kevin O'Connore54ee382009-07-26 19:33:13 -0400400
401 u16 arg1 = args[0];
402 dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
403
404 switch (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);
409 }
410}
411
412// romlayout.S
413extern void entry_pmm();
414
415void
416pmm_setup()
417{
418 if (! CONFIG_PMM)
419 return;
420
421 dprintf(3, "init PMM\n");
422
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400423 PMMAllocs = NULL;
424
Kevin O'Connore54ee382009-07-26 19:33:13 -0400425 PMMHEADER.signature = PMM_SIGNATURE;
426 PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
427 PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
428}
429
430void
431pmm_finalize()
432{
433 if (! CONFIG_PMM)
434 return;
435
436 dprintf(3, "finalize PMM\n");
437
438 PMMHEADER.signature = 0;
439 PMMHEADER.entry_offset = 0;
Kevin O'Connore54ee382009-07-26 19:33:13 -0400440}