Remove 16bit wrappers from PMM code.
[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" // struct e820entry
10 #include "farptr.h" // GET_FARVAR
11 #include "biosvar.h" // GET_BDA
12
13 // Information on a reserved area.
14 struct allocinfo_s {
15     struct allocinfo_s *next, **pprev;
16     void *data, *dataend, *allocend;
17 };
18
19 // Information on a tracked memory allocation.
20 struct allocdetail_s {
21     struct allocinfo_s detailinfo;
22     struct allocinfo_s datainfo;
23     u32 handle;
24 };
25
26 // The various memory zones.
27 struct zone_s {
28     struct allocinfo_s *info;
29 };
30
31 struct zone_s ZoneLow, ZoneHigh, ZoneFSeg, ZoneTmpLow, ZoneTmpHigh;
32
33 static struct zone_s *Zones[] = {
34     &ZoneTmpLow, &ZoneLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
35 };
36
37
38 /****************************************************************
39  * low-level memory reservations
40  ****************************************************************/
41
42 // Find and reserve space from a given zone
43 static void *
44 allocSpace(struct zone_s *zone, u32 size, u32 align, struct allocinfo_s *fill)
45 {
46     struct allocinfo_s *info;
47     for (info = zone->info; info; info = info->next) {
48         void *dataend = info->dataend;
49         void *allocend = info->allocend;
50         void *newallocend = (void*)ALIGN_DOWN((u32)allocend - size, align);
51         if (newallocend >= dataend && newallocend <= allocend) {
52             // Found space - now reserve it.
53             struct allocinfo_s **pprev = info->pprev;
54             if (!fill)
55                 fill = newallocend;
56             fill->next = info;
57             fill->pprev = pprev;
58             fill->data = newallocend;
59             fill->dataend = newallocend + size;
60             fill->allocend = allocend;
61
62             info->allocend = newallocend;
63             info->pprev = &fill->next;
64             *pprev = fill;
65             return newallocend;
66         }
67     }
68     return NULL;
69 }
70
71 // Release space allocated with allocSpace()
72 static void
73 freeSpace(struct allocinfo_s *info)
74 {
75     struct allocinfo_s *next = info->next;
76     struct allocinfo_s **pprev = info->pprev;
77     *pprev = next;
78     if (next) {
79         if (next->allocend == info->data)
80             next->allocend = info->allocend;
81         next->pprev = pprev;
82     }
83 }
84
85 // Add new memory to a zone
86 static void
87 addSpace(struct zone_s *zone, void *start, void *end)
88 {
89     // Find position to add space
90     struct allocinfo_s **pprev = &zone->info, *info;
91     for (;;) {
92         info = *pprev;
93         if (!info || info->data < start)
94             break;
95         pprev = &info->next;
96     }
97
98     // Add space using temporary allocation info.
99     struct allocdetail_s tempdetail;
100     tempdetail.datainfo.next = info;
101     tempdetail.datainfo.pprev = pprev;
102     tempdetail.datainfo.data = tempdetail.datainfo.dataend = start;
103     tempdetail.datainfo.allocend = end;
104     *pprev = &tempdetail.datainfo;
105     if (info)
106         info->pprev = &tempdetail.datainfo.next;
107
108     // Allocate final allocation info.
109     struct allocdetail_s *detail = allocSpace(
110         &ZoneTmpHigh, sizeof(*detail), MALLOC_MIN_ALIGN, NULL);
111     if (!detail) {
112         detail = allocSpace(&ZoneTmpLow, sizeof(*detail)
113                             , MALLOC_MIN_ALIGN, NULL);
114         if (!detail) {
115             *tempdetail.datainfo.pprev = tempdetail.datainfo.next;
116             if (tempdetail.datainfo.next)
117                 tempdetail.datainfo.next->pprev = tempdetail.datainfo.pprev;
118             warn_noalloc();
119             return;
120         }
121     }
122
123     // Replace temp alloc space with final alloc space
124     memcpy(&detail->datainfo, &tempdetail.datainfo, sizeof(detail->datainfo));
125     detail->handle = PMM_DEFAULT_HANDLE;
126
127     *tempdetail.datainfo.pprev = &detail->datainfo;
128     if (tempdetail.datainfo.next)
129         tempdetail.datainfo.next->pprev = &detail->datainfo.next;
130 }
131
132 // Search all zones for an allocation obtained from allocSpace()
133 static struct allocinfo_s *
134 findAlloc(void *data)
135 {
136     int i;
137     for (i=0; i<ARRAY_SIZE(Zones); i++) {
138         struct zone_s *zone = Zones[i];
139         struct allocinfo_s *info;
140         for (info = zone->info; info; info = info->next)
141             if (info->data == data)
142                 return info;
143     }
144     return NULL;
145 }
146
147 // Return the last sentinal node of a zone
148 static struct allocinfo_s *
149 findLast(struct zone_s *zone)
150 {
151     struct allocinfo_s *info = zone->info;
152     if (!info)
153         return NULL;
154     for (;;) {
155         struct allocinfo_s *next = info->next;
156         if (!next)
157             return info;
158         info = next;
159     }
160 }
161
162
163 /****************************************************************
164  * Setup
165  ****************************************************************/
166
167 void
168 malloc_setup(void)
169 {
170     ASSERT32FLAT();
171     dprintf(3, "malloc setup\n");
172
173     // Populate temp high ram
174     u32 highram = 0;
175     int i;
176     for (i=e820_count-1; i>=0; i--) {
177         struct e820entry *en = &e820_list[i];
178         u64 end = en->start + en->size;
179         if (end < 1024*1024)
180             break;
181         if (en->type != E820_RAM || end > 0xffffffff)
182             continue;
183         u32 s = en->start, e = end;
184         if (!highram) {
185             u32 newe = ALIGN_DOWN(e - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
186             if (newe <= e && newe >= s) {
187                 highram = newe;
188                 e = newe;
189             }
190         }
191         addSpace(&ZoneTmpHigh, (void*)s, (void*)e);
192     }
193
194     // Populate other regions
195     addSpace(&ZoneTmpLow, (void*)BUILD_STACK_ADDR, (void*)BUILD_EBDA_MINIMUM);
196     addSpace(&ZoneFSeg, BiosTableSpace, &BiosTableSpace[CONFIG_MAX_BIOSTABLE]);
197     addSpace(&ZoneLow, (void*)BUILD_LOWRAM_END, (void*)BUILD_LOWRAM_END);
198     if (highram) {
199         addSpace(&ZoneHigh, (void*)highram
200                  , (void*)highram + CONFIG_MAX_HIGHTABLE);
201         add_e820(highram, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
202     }
203 }
204
205 // Update pointers after code relocation.
206 void
207 malloc_fixupreloc(void)
208 {
209     ASSERT32FLAT();
210     if (!CONFIG_RELOCATE_INIT)
211         return;
212     dprintf(3, "malloc fixup reloc\n");
213
214     int i;
215     for (i=0; i<ARRAY_SIZE(Zones); i++) {
216         struct zone_s *zone = Zones[i];
217         zone->info->pprev = &zone->info;
218     }
219 }
220
221 void
222 malloc_finalize(void)
223 {
224     ASSERT32FLAT();
225     dprintf(3, "malloc finalize\n");
226
227     // Reserve more low-mem if needed.
228     u32 endlow = GET_BDA(mem_size_kb)*1024;
229     add_e820(endlow, BUILD_LOWRAM_END-endlow, E820_RESERVED);
230
231     // Give back unused high ram.
232     struct allocinfo_s *info = findLast(&ZoneHigh);
233     if (info) {
234         u32 giveback = ALIGN_DOWN(info->allocend - info->dataend, PAGE_SIZE);
235         add_e820((u32)info->dataend, giveback, E820_RAM);
236         dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
237     }
238 }
239
240
241 /****************************************************************
242  * ebda movement
243  ****************************************************************/
244
245 // Move ebda
246 static int
247 relocate_ebda(u32 newebda, u32 oldebda, u8 ebda_size)
248 {
249     u32 lowram = GET_BDA(mem_size_kb) * 1024;
250     if (oldebda != lowram)
251         // EBDA isn't at end of ram - give up.
252         return -1;
253
254     // Do copy
255     memmove((void*)newebda, (void*)oldebda, ebda_size * 1024);
256
257     // Update indexes
258     dprintf(1, "ebda moved from %x to %x\n", oldebda, newebda);
259     SET_BDA(mem_size_kb, newebda / 1024);
260     SET_BDA(ebda_seg, FLATPTR_TO_SEG(newebda));
261     return 0;
262 }
263
264 // Support expanding the ZoneLow dynamically.
265 static void
266 zonelow_expand(u32 size, u32 align)
267 {
268     struct allocinfo_s *info = findLast(&ZoneLow);
269     if (!info)
270         return;
271     u32 oldpos = (u32)info->allocend;
272     u32 newpos = ALIGN_DOWN(oldpos - size, align);
273     u32 bottom = (u32)info->dataend;
274     if (newpos >= bottom && newpos <= oldpos)
275         // Space already present.
276         return;
277     u16 ebda_seg = get_ebda_seg();
278     u32 ebda_pos = (u32)MAKE_FLATPTR(ebda_seg, 0);
279     u8 ebda_size = GET_EBDA2(ebda_seg, size);
280     u32 ebda_end = ebda_pos + ebda_size * 1024;
281     if (ebda_end != bottom)
282         // Something else is after ebda - can't use any existing space.
283         newpos = ALIGN_DOWN(ebda_end - size, align);
284     u32 newbottom = ALIGN_DOWN(newpos, 1024);
285     u32 newebda = ALIGN_DOWN(newbottom - ebda_size * 1024, 1024);
286     if (newebda < BUILD_EBDA_MINIMUM)
287         // Not enough space.
288         return;
289
290     // Move ebda
291     int ret = relocate_ebda(newebda, ebda_pos, ebda_size);
292     if (ret)
293         return;
294
295     // Update zone
296     if (ebda_end == bottom) {
297         info->data = (void*)newbottom;
298         info->dataend = (void*)newbottom;
299     } else
300         addSpace(&ZoneLow, (void*)newbottom, (void*)ebda_end);
301 }
302
303 // Check if can expand the given zone to fulfill an allocation
304 static void *
305 allocExpandSpace(struct zone_s *zone, u32 size, u32 align
306                  , struct allocinfo_s *fill)
307 {
308     void *data = allocSpace(zone, size, align, fill);
309     if (data || zone != &ZoneLow)
310         return data;
311
312     // Make sure to not move ebda while an optionrom is running.
313     if (unlikely(wait_preempt())) {
314         data = allocSpace(zone, size, align, fill);
315         if (data)
316             return data;
317     }
318
319     zonelow_expand(size, align);
320     return allocSpace(zone, size, align, fill);
321 }
322
323
324 /****************************************************************
325  * tracked memory allocations
326  ****************************************************************/
327
328 // Allocate memory from the given zone and track it as a PMM allocation
329 void * __malloc
330 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
331 {
332     ASSERT32FLAT();
333     if (!size)
334         return NULL;
335
336     // Find and reserve space for bookkeeping.
337     struct allocdetail_s *detail = allocSpace(
338         &ZoneTmpHigh, sizeof(*detail), MALLOC_MIN_ALIGN, NULL);
339     if (!detail) {
340         detail = allocSpace(&ZoneTmpLow, sizeof(*detail)
341                             , MALLOC_MIN_ALIGN, NULL);
342         if (!detail)
343             return NULL;
344     }
345
346     // Find and reserve space for main allocation
347     void *data = allocExpandSpace(zone, size, align, &detail->datainfo);
348     if (!data) {
349         freeSpace(&detail->detailinfo);
350         return NULL;
351     }
352
353     dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
354             " ret=%p (detail=%p)\n"
355             , zone, handle, size, align
356             , data, detail);
357     detail->handle = handle;
358
359     return data;
360 }
361
362 // Free a data block allocated with pmm_malloc
363 int
364 pmm_free(void *data)
365 {
366     ASSERT32FLAT();
367     struct allocinfo_s *info = findAlloc(data);
368     if (!info || data == (void*)info || data == info->dataend)
369         return -1;
370     struct allocdetail_s *detail = container_of(
371         info, struct allocdetail_s, datainfo);
372     dprintf(8, "pmm_free %p (detail=%p)\n", data, detail);
373     freeSpace(info);
374     freeSpace(&detail->detailinfo);
375     return 0;
376 }
377
378 // Find the amount of free space in a given zone.
379 static u32
380 pmm_getspace(struct zone_s *zone)
381 {
382     // XXX - doesn't account for ZoneLow being able to grow.
383     // XXX - results not reliable when CONFIG_THREAD_OPTIONROMS
384     u32 maxspace = 0;
385     struct allocinfo_s *info;
386     for (info = zone->info; info; info = info->next) {
387         u32 space = info->allocend - info->dataend;
388         if (space > maxspace)
389             maxspace = space;
390     }
391
392     if (zone != &ZoneTmpHigh && zone != &ZoneTmpLow)
393         return maxspace;
394     // Account for space needed for PMM tracking.
395     u32 reserve = ALIGN(sizeof(struct allocdetail_s), MALLOC_MIN_ALIGN);
396     if (maxspace <= reserve)
397         return 0;
398     return maxspace - reserve;
399 }
400
401 // Find the data block allocated with pmm_malloc with a given handle.
402 static void *
403 pmm_find(u32 handle)
404 {
405     int i;
406     for (i=0; i<ARRAY_SIZE(Zones); i++) {
407         struct zone_s *zone = Zones[i];
408         struct allocinfo_s *info;
409         for (info = zone->info; info; info = info->next) {
410             if (info->data != (void*)info)
411                 continue;
412             struct allocdetail_s *detail = container_of(
413                 info, struct allocdetail_s, detailinfo);
414             if (detail->handle == handle)
415                 return detail->datainfo.data;
416         }
417     }
418     return NULL;
419 }
420
421
422 /****************************************************************
423  * pmm interface
424  ****************************************************************/
425
426 struct pmmheader {
427     u32 signature;
428     u8 version;
429     u8 length;
430     u8 checksum;
431     u16 entry_offset;
432     u16 entry_seg;
433     u8 reserved[5];
434 } PACKED;
435
436 extern struct pmmheader PMMHEADER;
437
438 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
439
440 #if CONFIG_PMM
441 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
442     .version = 0x01,
443     .length = sizeof(PMMHEADER),
444     .entry_seg = SEG_BIOS,
445 };
446 #endif
447
448 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
449
450 // PMM - allocate
451 static u32
452 handle_pmm00(u16 *args)
453 {
454     u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
455     u16 flags = args[5];
456     dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
457             , length, handle, flags);
458     struct zone_s *lowzone = &ZoneTmpLow, *highzone = &ZoneTmpHigh;
459     if (flags & 8) {
460         // Permanent memory request.
461         lowzone = &ZoneLow;
462         highzone = &ZoneHigh;
463     }
464     if (!length) {
465         // Memory size request
466         switch (flags & 3) {
467         default:
468         case 0:
469             return 0;
470         case 1:
471             return pmm_getspace(lowzone);
472         case 2:
473             return pmm_getspace(highzone);
474         case 3: {
475             u32 spacelow = pmm_getspace(lowzone);
476             u32 spacehigh = pmm_getspace(highzone);
477             if (spacelow > spacehigh)
478                 return spacelow;
479             return spacehigh;
480         }
481         }
482     }
483     u32 size = length * 16;
484     if ((s32)size <= 0)
485         return 0;
486     u32 align = MALLOC_MIN_ALIGN;
487     if (flags & 4) {
488         align = 1<<__ffs(size);
489         if (align < MALLOC_MIN_ALIGN)
490             align = MALLOC_MIN_ALIGN;
491     }
492     switch (flags & 3) {
493     default:
494     case 0:
495         return 0;
496     case 1:
497         return (u32)pmm_malloc(lowzone, handle, size, align);
498     case 2:
499         return (u32)pmm_malloc(highzone, handle, size, align);
500     case 3: {
501         void *data = pmm_malloc(lowzone, handle, size, align);
502         if (data)
503             return (u32)data;
504         return (u32)pmm_malloc(highzone, handle, size, align);
505     }
506     }
507 }
508
509 // PMM - find
510 static u32
511 handle_pmm01(u16 *args)
512 {
513     u32 handle = *(u32*)&args[1];
514     dprintf(3, "pmm01: handle=%x\n", handle);
515     if (handle == PMM_DEFAULT_HANDLE)
516         return 0;
517     return (u32)pmm_find(handle);
518 }
519
520 // PMM - deallocate
521 static u32
522 handle_pmm02(u16 *args)
523 {
524     u32 buffer = *(u32*)&args[1];
525     dprintf(3, "pmm02: buffer=%x\n", buffer);
526     int ret = pmm_free((void*)buffer);
527     if (ret)
528         // Error
529         return 1;
530     return 0;
531 }
532
533 static u32
534 handle_pmmXX(u16 *args)
535 {
536     return PMM_FUNCTION_NOT_SUPPORTED;
537 }
538
539 u32 VISIBLE32INIT
540 handle_pmm(u16 *args)
541 {
542     ASSERT32FLAT();
543     if (! CONFIG_PMM)
544         return PMM_FUNCTION_NOT_SUPPORTED;
545
546     u16 arg1 = args[0];
547     dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
548
549     int oldpreempt;
550     if (CONFIG_THREAD_OPTIONROMS) {
551         // Not a preemption event - don't wait in wait_preempt()
552         oldpreempt = CanPreempt;
553         CanPreempt = 0;
554     }
555
556     u32 ret;
557     switch (arg1) {
558     case 0x00: ret = handle_pmm00(args); break;
559     case 0x01: ret = handle_pmm01(args); break;
560     case 0x02: ret = handle_pmm02(args); break;
561     default:   ret = handle_pmmXX(args); break;
562     }
563
564     if (CONFIG_THREAD_OPTIONROMS)
565         CanPreempt = oldpreempt;
566
567     return ret;
568 }
569
570 // romlayout.S
571 extern void entry_pmm(void);
572
573 void
574 pmm_setup(void)
575 {
576     if (! CONFIG_PMM)
577         return;
578
579     dprintf(3, "init PMM\n");
580
581     PMMHEADER.signature = PMM_SIGNATURE;
582     PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
583     PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
584 }
585
586 void
587 pmm_finalize(void)
588 {
589     if (! CONFIG_PMM)
590         return;
591
592     dprintf(3, "finalize PMM\n");
593
594     PMMHEADER.signature = 0;
595     PMMHEADER.entry_offset = 0;
596 }