blob: 3b4aa07ccc06d865484f846f2c6cb0d429a02917 [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'Connora3855ad2009-08-16 21:59:40 -04009#include "memmap.h" // find_high_area
Kevin O'Connor0bf92702009-08-01 11:45:37 -040010#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
Kevin O'Connor0e9fd612009-08-22 21:31:58 -040033struct zone_s ZoneLow VAR32VISIBLE, ZoneHigh VAR32VISIBLE;
34struct zone_s ZoneFSeg VAR32VISIBLE;
Kevin O'Connor0bf92702009-08-01 11:45:37 -040035struct zone_s ZoneTmpLow VAR32VISIBLE, ZoneTmpHigh VAR32VISIBLE;
36
37struct zone_s *Zones[] VAR32VISIBLE = {
Kevin O'Connor0e9fd612009-08-22 21:31:58 -040038 &ZoneTmpLow, &ZoneLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
Kevin O'Connor0bf92702009-08-01 11:45:37 -040039};
40
41// Obtain memory from a given zone.
42static void *
43zone_malloc(struct zone_s *zone, u32 size, u32 align)
44{
45 u32 newpos = (GET_PMMVAR(zone->cur) - size) / align * align;
46 if ((s32)(newpos - GET_PMMVAR(zone->bottom)) < 0)
47 // No space
48 return NULL;
49 SET_PMMVAR(zone->cur, newpos);
50 return (void*)newpos;
51}
52
53// Return memory to a zone (if it was the last to be allocated).
54static void
55zone_free(struct zone_s *zone, void *data, u32 olddata)
56{
57 if (! data || GET_PMMVAR(zone->cur) != (u32)data)
58 return;
59 SET_PMMVAR(zone->cur, olddata);
60}
61
62// Find the zone that contains the given data block.
63static struct zone_s *
64zone_find(void *data)
65{
66 int i;
67 for (i=0; i<ARRAY_SIZE(Zones); i++) {
68 struct zone_s *zone = GET_PMMVAR(Zones[i]);
69 if ((u32)data >= GET_PMMVAR(zone->cur)
70 && (u32)data < GET_PMMVAR(zone->top))
71 return zone;
72 }
73 return NULL;
74}
75
76// Report the status of all the zones.
77static void
78dumpZones()
79{
80 int i;
81 for (i=0; i<ARRAY_SIZE(Zones); i++) {
82 struct zone_s *zone = Zones[i];
83 u32 used = zone->top - zone->cur;
84 u32 avail = zone->top - zone->bottom;
Kevin O'Connor9c3e7472009-08-02 12:33:58 -040085 u32 pct = avail ? ((100 * used) / avail) : 0;
Kevin O'Connor0bf92702009-08-01 11:45:37 -040086 dprintf(2, "zone %d: %08x-%08x used=%d (%d%%)\n"
Kevin O'Connor9c3e7472009-08-02 12:33:58 -040087 , i, zone->bottom, zone->top, used, pct);
Kevin O'Connor0bf92702009-08-01 11:45:37 -040088 }
89}
90
91// Allocate memory at the top of 32bit ram.
92void *
93malloc_high(u32 size)
94{
95 return zone_malloc(&ZoneHigh, size, MALLOC_MIN_ALIGN);
96}
97
98// Allocate memory in the 0xf0000-0x100000 area of ram.
99void *
100malloc_fseg(u32 size)
101{
102 return zone_malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
103}
104
105void
106malloc_setup()
107{
108 ASSERT32();
109 dprintf(3, "malloc setup\n");
110
111 // Memory in 0xf0000 area.
112 memset(BiosTableSpace, 0, CONFIG_MAX_BIOSTABLE);
113 ZoneFSeg.bottom = (u32)BiosTableSpace;
114 ZoneFSeg.top = ZoneFSeg.cur = ZoneFSeg.bottom + CONFIG_MAX_BIOSTABLE;
115
116 // Memory under 1Meg.
117 ZoneTmpLow.bottom = BUILD_STACK_ADDR;
118 ZoneTmpLow.top = ZoneTmpLow.cur = (u32)MAKE_FLATPTR(EBDA_SEGMENT_MINIMUM, 0);
119
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400120 // Permanent memory under 1Meg. XXX - not implemented yet.
121 ZoneLow.bottom = ZoneLow.top = ZoneLow.cur = 0xa0000;
122
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400123 // Find memory at the top of ram.
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400124 struct e820entry *e = find_high_area(CONFIG_MAX_HIGHTABLE+MALLOC_MIN_ALIGN);
125 if (!e) {
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400126 // No memory above 1Meg
127 memset(&ZoneHigh, 0, sizeof(ZoneHigh));
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400128 memset(&ZoneTmpHigh, 0, sizeof(ZoneTmpHigh));
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400129 return;
130 }
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400131 u32 top = e->start + e->size, bottom = e->start;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400132
133 // Memory at top of ram.
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400134 ZoneHigh.bottom = ALIGN(top - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400135 ZoneHigh.top = ZoneHigh.cur = ZoneHigh.bottom + CONFIG_MAX_HIGHTABLE;
136 add_e820(ZoneHigh.bottom, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
137
138 // Memory above 1Meg
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400139 ZoneTmpHigh.bottom = ALIGN(bottom, MALLOC_MIN_ALIGN);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400140 ZoneTmpHigh.top = ZoneTmpHigh.cur = ZoneHigh.bottom;
141}
142
143void
144malloc_finalize()
145{
146 dprintf(3, "malloc finalize\n");
147
148 dumpZones();
149
150 // Give back unused high ram.
151 u32 giveback = (ZoneHigh.cur - ZoneHigh.bottom) / 4096 * 4096;
152 add_e820(ZoneHigh.bottom, giveback, E820_RAM);
153 dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
154
155 // Clear low-memory allocations.
156 memset((void*)ZoneTmpLow.bottom, 0, ZoneTmpLow.top - ZoneTmpLow.bottom);
157}
158
159
160/****************************************************************
161 * pmm allocation
162 ****************************************************************/
163
164// Information on PMM tracked allocations
165struct pmmalloc_s {
166 void *data;
167 u32 olddata;
168 u32 handle;
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400169 u32 oldallocdata;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400170 struct pmmalloc_s *next;
171};
172
173struct pmmalloc_s *PMMAllocs VAR32VISIBLE;
174
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400175// Memory zone that pmm allocation tracking info is stored in
176#define ZONEALLOC (&ZoneTmpHigh)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400177
178// Allocate memory from the given zone and track it as a PMM allocation
179static void *
180pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
181{
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400182 u32 oldallocdata = GET_PMMVAR(ZONEALLOC->cur);
183 struct pmmalloc_s *info = zone_malloc(ZONEALLOC, sizeof(*info)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400184 , MALLOC_MIN_ALIGN);
185 if (!info)
186 return NULL;
187 u32 olddata = GET_PMMVAR(zone->cur);
188 void *data = zone_malloc(zone, size, align);
189 if (! data) {
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400190 zone_free(ZONEALLOC, info, oldallocdata);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400191 return NULL;
192 }
193 dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
194 " ret=%p (info=%p)\n"
195 , zone, handle, size, align
196 , data, info);
197 SET_PMMVAR(info->data, data);
198 SET_PMMVAR(info->olddata, olddata);
199 SET_PMMVAR(info->handle, handle);
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400200 SET_PMMVAR(info->oldallocdata, oldallocdata);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400201 SET_PMMVAR(info->next, GET_PMMVAR(PMMAllocs));
202 SET_PMMVAR(PMMAllocs, info);
203 return data;
204}
205
206// Free a raw data block (either from a zone or from pmm alloc list).
207static void
208pmm_free_data(struct zone_s *zone, void *data, u32 olddata)
209{
210 if (GET_PMMVAR(zone->cur) == (u32)data) {
211 zone_free(zone, data, olddata);
212 return;
213 }
214 struct pmmalloc_s *info;
215 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
216 if (GET_PMMVAR(info->olddata) == (u32)data) {
217 SET_PMMVAR(info->olddata, olddata);
218 return;
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400219 } else if (GET_PMMVAR(info->oldallocdata) == (u32)data) {
220 SET_PMMVAR(info->oldallocdata, olddata);
221 return;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400222 }
223}
224
225// Free a data block allocated with pmm_malloc
226static int
227pmm_free(void *data)
228{
229 struct zone_s *zone = zone_find(GET_PMMVAR(data));
230 if (!zone)
231 return -1;
232 struct pmmalloc_s **pinfo = &PMMAllocs;
233 for (;;) {
234 struct pmmalloc_s *info = GET_PMMVAR(*pinfo);
235 if (!info)
236 return -1;
237 if (GET_PMMVAR(info->data) == data) {
238 SET_PMMVAR(*pinfo, GET_PMMVAR(info->next));
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400239 u32 oldallocdata = GET_PMMVAR(info->oldallocdata);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400240 u32 olddata = GET_PMMVAR(info->olddata);
241 pmm_free_data(zone, data, olddata);
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400242 pmm_free_data(ZONEALLOC, info, oldallocdata);
243 dprintf(8, "pmm_free data=%p zone=%p olddata=%p oldallocdata=%p"
244 " info=%p\n"
245 , data, zone, (void*)olddata, (void*)oldallocdata
246 , info);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400247 return 0;
248 }
249 pinfo = &info->next;
250 }
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400251}
252
253// Find the amount of free space in a given zone.
254static u32
255pmm_getspace(struct zone_s *zone)
256{
257 u32 space = GET_PMMVAR(zone->cur) - GET_PMMVAR(zone->bottom);
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400258 if (zone != ZONEALLOC)
259 return space;
260 u32 reserve = ALIGN(sizeof(struct pmmalloc_s), MALLOC_MIN_ALIGN);
261 if (space <= reserve)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400262 return 0;
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400263 return space - reserve;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400264}
265
266// Find the data block allocated with pmm_malloc with a given handle.
267static void *
268pmm_find(u32 handle)
269{
270 struct pmmalloc_s *info;
271 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
272 if (GET_PMMVAR(info->handle) == handle)
273 return GET_PMMVAR(info->data);
274 return NULL;
275}
276
277
278/****************************************************************
279 * pmm interface
280 ****************************************************************/
Kevin O'Connore54ee382009-07-26 19:33:13 -0400281
282struct pmmheader {
283 u32 signature;
284 u8 version;
285 u8 length;
286 u8 checksum;
287 u16 entry_offset;
288 u16 entry_seg;
289 u8 reserved[5];
290} PACKED;
291
292extern struct pmmheader PMMHEADER;
293
294#define PMM_SIGNATURE 0x4d4d5024 // $PMM
295
296#if CONFIG_PMM
297struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
298 .version = 0x01,
299 .length = sizeof(PMMHEADER),
300 .entry_seg = SEG_BIOS,
301};
302#endif
303
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400304#define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
Kevin O'Connore54ee382009-07-26 19:33:13 -0400305
306// PMM - allocate
307static u32
308handle_pmm00(u16 *args)
309{
310 u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
311 u16 flags = args[5];
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400312 dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
Kevin O'Connore54ee382009-07-26 19:33:13 -0400313 , length, handle, flags);
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400314 struct zone_s *lowzone = &ZoneTmpLow, *highzone = &ZoneTmpHigh;
315 if (flags & 8) {
316 // Permanent memory request.
317 lowzone = &ZoneLow;
318 highzone = &ZoneHigh;
319 }
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:
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400327 return pmm_getspace(lowzone);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400328 case 2:
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400329 return pmm_getspace(highzone);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400330 case 3: {
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400331 u32 spacelow = pmm_getspace(lowzone);
332 u32 spacehigh = pmm_getspace(highzone);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400333 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:
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400353 return (u32)pmm_malloc(lowzone, handle, size, align);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400354 case 2:
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400355 return (u32)pmm_malloc(highzone, handle, size, align);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400356 case 3: {
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400357 void *data = pmm_malloc(lowzone, handle, size, align);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400358 if (data)
359 return (u32)data;
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400360 return (u32)pmm_malloc(highzone, handle, size, align);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400361 }
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}