Add support for using floppy images in CBFS.
[seabios.git] / src / pmm.c
1 // 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
9 #include "memmap.h" // find_high_area
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
29 struct zone_s {
30     u32 top, bottom, cur;
31 };
32
33 struct zone_s ZoneHigh VAR32VISIBLE, ZoneFSeg VAR32VISIBLE;
34 struct zone_s ZoneTmpLow VAR32VISIBLE, ZoneTmpHigh VAR32VISIBLE;
35
36 struct zone_s *Zones[] VAR32VISIBLE = {
37     &ZoneTmpLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
38 };
39
40 // Obtain memory from a given zone.
41 static void *
42 zone_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).
53 static void
54 zone_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.
62 static struct zone_s *
63 zone_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.
76 static void
77 dumpZones()
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;
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);
87     }
88 }
89
90 // Allocate memory at the top of 32bit ram.
91 void *
92 malloc_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.
98 void *
99 malloc_fseg(u32 size)
100 {
101     return zone_malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
102 }
103
104 void
105 malloc_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     struct e820entry *e = find_high_area(CONFIG_MAX_HIGHTABLE+MALLOC_MIN_ALIGN);
121     if (!e) {
122         // No memory above 1Meg
123         memset(&ZoneHigh, 0, sizeof(ZoneHigh));
124         memset(&ZoneTmpHigh, 0, sizeof(ZoneTmpHigh));
125         return;
126     }
127     u32 top = e->start + e->size, bottom = e->start;
128
129     // Memory at top of ram.
130     ZoneHigh.bottom = ALIGN(top - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
131     ZoneHigh.top = ZoneHigh.cur = ZoneHigh.bottom + CONFIG_MAX_HIGHTABLE;
132     add_e820(ZoneHigh.bottom, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
133
134     // Memory above 1Meg
135     ZoneTmpHigh.bottom = ALIGN(bottom, MALLOC_MIN_ALIGN);
136     ZoneTmpHigh.top = ZoneTmpHigh.cur = ZoneHigh.bottom;
137 }
138
139 void
140 malloc_finalize()
141 {
142     dprintf(3, "malloc finalize\n");
143
144     dumpZones();
145
146     // Give back unused high ram.
147     u32 giveback = (ZoneHigh.cur - ZoneHigh.bottom) / 4096 * 4096;
148     add_e820(ZoneHigh.bottom, giveback, E820_RAM);
149     dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
150
151     // Clear low-memory allocations.
152     memset((void*)ZoneTmpLow.bottom, 0, ZoneTmpLow.top - ZoneTmpLow.bottom);
153 }
154
155
156 /****************************************************************
157  * pmm allocation
158  ****************************************************************/
159
160 // Information on PMM tracked allocations
161 struct pmmalloc_s {
162     void *data;
163     u32 olddata;
164     u32 handle;
165     u32 oldallocdata;
166     struct pmmalloc_s *next;
167 };
168
169 struct pmmalloc_s *PMMAllocs VAR32VISIBLE;
170
171 // Memory zone that pmm allocation tracking info is stored in
172 #define ZONEALLOC (&ZoneTmpHigh)
173
174 // Allocate memory from the given zone and track it as a PMM allocation
175 static void *
176 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
177 {
178     u32 oldallocdata = GET_PMMVAR(ZONEALLOC->cur);
179     struct pmmalloc_s *info = zone_malloc(ZONEALLOC, sizeof(*info)
180                                           , MALLOC_MIN_ALIGN);
181     if (!info)
182         return NULL;
183     u32 olddata = GET_PMMVAR(zone->cur);
184     void *data = zone_malloc(zone, size, align);
185     if (! data) {
186         zone_free(ZONEALLOC, info, oldallocdata);
187         return NULL;
188     }
189     dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
190             " ret=%p (info=%p)\n"
191             , zone, handle, size, align
192             , data, info);
193     SET_PMMVAR(info->data, data);
194     SET_PMMVAR(info->olddata, olddata);
195     SET_PMMVAR(info->handle, handle);
196     SET_PMMVAR(info->oldallocdata, oldallocdata);
197     SET_PMMVAR(info->next, GET_PMMVAR(PMMAllocs));
198     SET_PMMVAR(PMMAllocs, info);
199     return data;
200 }
201
202 // Free a raw data block (either from a zone or from pmm alloc list).
203 static void
204 pmm_free_data(struct zone_s *zone, void *data, u32 olddata)
205 {
206     if (GET_PMMVAR(zone->cur) == (u32)data) {
207         zone_free(zone, data, olddata);
208         return;
209     }
210     struct pmmalloc_s *info;
211     for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
212         if (GET_PMMVAR(info->olddata) == (u32)data) {
213             SET_PMMVAR(info->olddata, olddata);
214             return;
215         } else if (GET_PMMVAR(info->oldallocdata) == (u32)data) {
216             SET_PMMVAR(info->oldallocdata, olddata);
217             return;
218         }
219 }
220
221 // Free a data block allocated with pmm_malloc
222 static int
223 pmm_free(void *data)
224 {
225     struct zone_s *zone = zone_find(GET_PMMVAR(data));
226     if (!zone)
227         return -1;
228     struct pmmalloc_s **pinfo = &PMMAllocs;
229     for (;;) {
230         struct pmmalloc_s *info = GET_PMMVAR(*pinfo);
231         if (!info)
232             return -1;
233         if (GET_PMMVAR(info->data) == data) {
234             SET_PMMVAR(*pinfo, GET_PMMVAR(info->next));
235             u32 oldallocdata = GET_PMMVAR(info->oldallocdata);
236             u32 olddata = GET_PMMVAR(info->olddata);
237             pmm_free_data(zone, data, olddata);
238             pmm_free_data(ZONEALLOC, info, oldallocdata);
239             dprintf(8, "pmm_free data=%p zone=%p olddata=%p oldallocdata=%p"
240                     " info=%p\n"
241                     , data, zone, (void*)olddata, (void*)oldallocdata
242                     , info);
243             return 0;
244         }
245         pinfo = &info->next;
246     }
247 }
248
249 // Find the amount of free space in a given zone.
250 static u32
251 pmm_getspace(struct zone_s *zone)
252 {
253     u32 space = GET_PMMVAR(zone->cur) - GET_PMMVAR(zone->bottom);
254     if (zone != ZONEALLOC)
255         return space;
256     u32 reserve = ALIGN(sizeof(struct pmmalloc_s), MALLOC_MIN_ALIGN);
257     if (space <= reserve)
258         return 0;
259     return space - reserve;
260 }
261
262 // Find the data block allocated with pmm_malloc with a given handle.
263 static void *
264 pmm_find(u32 handle)
265 {
266     struct pmmalloc_s *info;
267     for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
268         if (GET_PMMVAR(info->handle) == handle)
269             return GET_PMMVAR(info->data);
270     return NULL;
271 }
272
273
274 /****************************************************************
275  * pmm interface
276  ****************************************************************/
277
278 struct pmmheader {
279     u32 signature;
280     u8 version;
281     u8 length;
282     u8 checksum;
283     u16 entry_offset;
284     u16 entry_seg;
285     u8 reserved[5];
286 } PACKED;
287
288 extern struct pmmheader PMMHEADER;
289
290 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
291
292 #if CONFIG_PMM
293 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
294     .version = 0x01,
295     .length = sizeof(PMMHEADER),
296     .entry_seg = SEG_BIOS,
297 };
298 #endif
299
300 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
301
302 // PMM - allocate
303 static u32
304 handle_pmm00(u16 *args)
305 {
306     u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
307     u16 flags = args[5];
308     dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
309             , length, handle, flags);
310     if (!length) {
311         // Memory size request
312         switch (flags & 3) {
313         default:
314         case 0:
315             return 0;
316         case 1:
317             return pmm_getspace(&ZoneTmpLow);
318         case 2:
319             return pmm_getspace(&ZoneTmpHigh);
320         case 3: {
321             u32 spacelow = pmm_getspace(&ZoneTmpLow);
322             u32 spacehigh = pmm_getspace(&ZoneTmpHigh);
323             if (spacelow > spacehigh)
324                 return spacelow;
325             return spacehigh;
326         }
327         }
328     }
329     u32 size = length * 16;
330     if ((s32)size <= 0)
331         return 0;
332     u32 align = MALLOC_MIN_ALIGN;
333     if (flags & 4) {
334         align = 1<<__ffs(size);
335         if (align < MALLOC_MIN_ALIGN)
336             align = MALLOC_MIN_ALIGN;
337     }
338     switch (flags & 3) {
339     default:
340     case 0:
341         return 0;
342     case 1:
343         return (u32)pmm_malloc(&ZoneTmpLow, handle, size, align);
344     case 2:
345         return (u32)pmm_malloc(&ZoneTmpHigh, handle, size, align);
346     case 3: {
347         void *data = pmm_malloc(&ZoneTmpLow, handle, size, align);
348         if (data)
349             return (u32)data;
350         return (u32)pmm_malloc(&ZoneTmpHigh, handle, size, align);
351     }
352     }
353 }
354
355 // PMM - find
356 static u32
357 handle_pmm01(u16 *args)
358 {
359     u32 handle = *(u32*)&args[1];
360     dprintf(3, "pmm01: handle=%x\n", handle);
361     if (handle == 0xFFFFFFFF)
362         return 0;
363     return (u32)pmm_find(handle);
364 }
365
366 // PMM - deallocate
367 static u32
368 handle_pmm02(u16 *args)
369 {
370     u32 buffer = *(u32*)&args[1];
371     dprintf(3, "pmm02: buffer=%x\n", buffer);
372     int ret = pmm_free((void*)buffer);
373     if (ret)
374         // Error
375         return 1;
376     return 0;
377 }
378
379 static u32
380 handle_pmmXX(u16 *args)
381 {
382     return PMM_FUNCTION_NOT_SUPPORTED;
383 }
384
385 u32 VISIBLE16
386 handle_pmm(u16 *args)
387 {
388     if (! CONFIG_PMM)
389         return PMM_FUNCTION_NOT_SUPPORTED;
390
391     u16 arg1 = args[0];
392     dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
393
394     switch (arg1) {
395     case 0x00: return handle_pmm00(args);
396     case 0x01: return handle_pmm01(args);
397     case 0x02: return handle_pmm02(args);
398     default:   return handle_pmmXX(args);
399     }
400 }
401
402 // romlayout.S
403 extern void entry_pmm();
404
405 void
406 pmm_setup()
407 {
408     if (! CONFIG_PMM)
409         return;
410
411     dprintf(3, "init PMM\n");
412
413     PMMAllocs = NULL;
414
415     PMMHEADER.signature = PMM_SIGNATURE;
416     PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
417     PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
418 }
419
420 void
421 pmm_finalize()
422 {
423     if (! CONFIG_PMM)
424         return;
425
426     dprintf(3, "finalize PMM\n");
427
428     PMMHEADER.signature = 0;
429     PMMHEADER.entry_offset = 0;
430 }