~jonathank89/burg/burg-percise

1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
1
/* Memory management for efiemu */
2
/*
3
 *  GRUB  --  GRand Unified Bootloader
4
 *  Copyright (C) 2009  Free Software Foundation, Inc.
5
 *
6
 *  GRUB is free software: you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation, either version 3 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  GRUB is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
/*
1419 by fzielcke
remove all trailing whitespace
20
  To keep efiemu runtime contiguous this mm is special.
1310 by proski
2009-05-04 Pavel Roskin <proski@gnu.org>
21
  It uses deferred allocation.
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
22
  In the first stage you may request memory with grub_efiemu_request_memalign
1419 by fzielcke
remove all trailing whitespace
23
  It will give you a handle with which in the second phase you can access your
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
24
  memory with grub_efiemu_mm_obtain_request (handle). It's guaranteed that
25
  subsequent calls with the same handle return the same result. You can't request any additional memory once you're in the second phase
26
*/
27
28
#include <grub/err.h>
29
#include <grub/normal.h>
30
#include <grub/mm.h>
31
#include <grub/misc.h>
32
#include <grub/machine/memory.h>
33
#include <grub/efiemu/efiemu.h>
34
1761 by Bean
Initial upload for BURG.
35
GRUB_EXPORT(grub_efiemu_get_memory_map);
36
1419 by fzielcke
remove all trailing whitespace
37
struct grub_efiemu_memrequest
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
38
{
39
  struct grub_efiemu_memrequest *next;
40
  grub_efi_memory_type_t type;
41
  grub_size_t size;
42
  grub_size_t align_overhead;
43
  int handle;
44
  void *val;
45
};
46
/* Linked list of requested memory. */
47
static struct grub_efiemu_memrequest *memrequests = 0;
48
/* Memory map. */
49
static grub_efi_memory_descriptor_t *efiemu_mmap = 0;
50
/* Pointer to allocated memory */
51
static void *resident_memory = 0;
52
/* Size of requested memory per type */
53
static grub_size_t requested_memory[GRUB_EFI_MAX_MEMORY_TYPE];
54
/* How many slots is allocated for memory_map and how many are already used */
55
static int mmap_reserved_size = 0, mmap_num = 0;
56
57
/* Add a memory region to map*/
58
static grub_err_t
1419 by fzielcke
remove all trailing whitespace
59
grub_efiemu_add_to_mmap (grub_uint64_t start, grub_uint64_t size,
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
60
			 grub_efi_memory_type_t type)
61
{
62
  grub_uint64_t page_start, npages;
63
64
  /* Extend map if necessary*/
65
  if (mmap_num >= mmap_reserved_size)
66
    {
67
      efiemu_mmap = (grub_efi_memory_descriptor_t *)
1419 by fzielcke
remove all trailing whitespace
68
	grub_realloc (efiemu_mmap, (++mmap_reserved_size)
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
69
		      * sizeof (grub_efi_memory_descriptor_t));
70
      if (!efiemu_mmap)
1419 by fzielcke
remove all trailing whitespace
71
	return grub_error (GRUB_ERR_OUT_OF_MEMORY,
1760.1.221 by carles
2009-12-24 Carles Pina i Estany <carles@pina.cat>
72
			   "not enough space for memory map");
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
73
    }
74
75
  /* Fill slot*/
76
  page_start = start - (start % GRUB_EFIEMU_PAGESIZE);
77
  npages = (size + (start % GRUB_EFIEMU_PAGESIZE) + GRUB_EFIEMU_PAGESIZE - 1)
78
    / GRUB_EFIEMU_PAGESIZE;
1419 by fzielcke
remove all trailing whitespace
79
  efiemu_mmap[mmap_num].physical_start = page_start;
80
  efiemu_mmap[mmap_num].virtual_start = page_start;
81
  efiemu_mmap[mmap_num].num_pages = npages;
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
82
  efiemu_mmap[mmap_num].type = type;
83
  mmap_num++;
84
85
  return GRUB_ERR_NONE;
86
}
87
88
/* Request a resident memory of type TYPE of size SIZE aligned at ALIGN
1419 by fzielcke
remove all trailing whitespace
89
   ALIGN must be a divisor of page size (if it's a divisor of 4096
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
90
   it should be ok on all platforms)
91
 */
92
int
1419 by fzielcke
remove all trailing whitespace
93
grub_efiemu_request_memalign (grub_size_t align, grub_size_t size,
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
94
			      grub_efi_memory_type_t type)
95
{
96
  grub_size_t align_overhead;
97
  struct grub_efiemu_memrequest *ret, *cur, *prev;
98
  /* Check that the request is correct */
99
  if (type >= GRUB_EFI_MAX_MEMORY_TYPE || type <= GRUB_EFI_LOADER_CODE)
100
    return -2;
101
102
  /* Add new size to requested size */
103
  align_overhead = align - (requested_memory[type]%align);
104
  if (align_overhead == align)
105
    align_overhead = 0;
106
  requested_memory[type] += align_overhead + size;
107
108
  /* Remember the request */
1546 by proski
2009-07-16 Pavel Roskin <proski@gnu.org>
109
  ret = grub_zalloc (sizeof (*ret));
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
110
  if (!ret)
111
    return -1;
112
  ret->type = type;
113
  ret->size = size;
114
  ret->align_overhead = align_overhead;
115
  prev = 0;
1419 by fzielcke
remove all trailing whitespace
116
117
  /* Add request to the end of the chain.
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
118
     It should be at the end because otherwise alignment isn't guaranteed */
119
  for (cur = memrequests; cur; prev = cur, cur = cur->next);
120
  if (prev)
121
    {
122
      ret->handle = prev->handle + 1;
123
      prev->next = ret;
124
    }
125
  else
126
    {
127
      ret->handle = 1; /* Avoid 0 handle*/
128
      memrequests = ret;
129
    }
130
  return ret->handle;
131
}
132
133
/* Really allocate the memory */
134
static grub_err_t
135
efiemu_alloc_requests (void)
136
{
137
  grub_size_t align_overhead = 0;
138
  grub_uint8_t *curptr, *typestart;
139
  struct grub_efiemu_memrequest *cur;
140
  grub_size_t total_alloc = 0;
141
  unsigned i;
142
  /* Order of memory regions */
143
  grub_efi_memory_type_t reqorder[] =
144
    {
145
      /* First come regions usable by OS*/
146
      GRUB_EFI_LOADER_CODE,
147
      GRUB_EFI_LOADER_DATA,
148
      GRUB_EFI_BOOT_SERVICES_CODE,
149
      GRUB_EFI_BOOT_SERVICES_DATA,
150
      GRUB_EFI_CONVENTIONAL_MEMORY,
151
      GRUB_EFI_ACPI_RECLAIM_MEMORY,
152
153
      /* Then memory used by runtime */
154
      /* This way all our regions are in a single block */
155
      GRUB_EFI_RUNTIME_SERVICES_CODE,
156
      GRUB_EFI_RUNTIME_SERVICES_DATA,
157
      GRUB_EFI_ACPI_MEMORY_NVS,
1419 by fzielcke
remove all trailing whitespace
158
159
      /* And then unavailable memory types. This is more for a completeness.
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
160
	 You should double think before allocating memory of any of these types
161
       */
162
      GRUB_EFI_UNUSABLE_MEMORY,
163
      GRUB_EFI_MEMORY_MAPPED_IO,
164
      GRUB_EFI_MEMORY_MAPPED_IO_PORT_SPACE,
1419 by fzielcke
remove all trailing whitespace
165
      GRUB_EFI_PAL_CODE
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
166
    };
167
168
  /* Compute total memory needed */
169
  for (i = 0; i < sizeof (reqorder) / sizeof (reqorder[0]); i++)
170
    {
1419 by fzielcke
remove all trailing whitespace
171
      align_overhead = GRUB_EFIEMU_PAGESIZE
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
172
	- (requested_memory[reqorder[i]] % GRUB_EFIEMU_PAGESIZE);
173
      if (align_overhead == GRUB_EFIEMU_PAGESIZE)
174
	align_overhead = 0;
175
      total_alloc += requested_memory[reqorder[i]] + align_overhead;
176
    }
177
178
  /* Allocate the whole memory in one block */
179
  resident_memory = grub_memalign (GRUB_EFIEMU_PAGESIZE, total_alloc);
180
  if (!resident_memory)
1419 by fzielcke
remove all trailing whitespace
181
    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
182
		       "couldn't allocate resident memory");
183
184
  /* Split the memory into blocks by type */
185
  curptr = resident_memory;
186
  for (i = 0; i < sizeof (reqorder) / sizeof (reqorder[0]); i++)
187
    {
188
      if (!requested_memory[reqorder[i]])
189
	continue;
190
      typestart = curptr;
191
192
      /* Write pointers to requests */
193
      for (cur = memrequests; cur; cur = cur->next)
194
	if (cur->type == reqorder[i])
195
	  {
196
	    curptr = ((grub_uint8_t *)curptr) + cur->align_overhead;
197
	    cur->val = curptr;
198
	    curptr = ((grub_uint8_t *)curptr) + cur->size;
199
	  }
200
201
      /* Ensure that the regions are page-aligned */
1419 by fzielcke
remove all trailing whitespace
202
      align_overhead = GRUB_EFIEMU_PAGESIZE
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
203
	- (requested_memory[reqorder[i]] % GRUB_EFIEMU_PAGESIZE);
204
      if (align_overhead == GRUB_EFIEMU_PAGESIZE)
205
	align_overhead = 0;
206
      curptr = ((grub_uint8_t *)curptr) + align_overhead;
1419 by fzielcke
remove all trailing whitespace
207
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
208
      /* Add the region to memory map */
1419 by fzielcke
remove all trailing whitespace
209
      grub_efiemu_add_to_mmap (PTR_TO_UINT64 (typestart),
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
210
			       curptr - typestart, reqorder[i]);
211
    }
212
213
  return GRUB_ERR_NONE;
214
}
215
216
/* Get a pointer to requested memory from handle */
217
void *
218
grub_efiemu_mm_obtain_request (int handle)
219
{
220
  struct grub_efiemu_memrequest *cur;
221
  for (cur = memrequests; cur; cur = cur->next)
222
    if (cur->handle == handle)
223
      return cur->val;
224
  return 0;
225
}
226
227
/* Get type of requested memory by handle */
228
grub_efi_memory_type_t
229
grub_efiemu_mm_get_type (int handle)
230
{
231
  struct grub_efiemu_memrequest *cur;
232
  for (cur = memrequests; cur; cur = cur->next)
233
    if (cur->handle == handle)
234
      return cur->type;
235
  return 0;
236
}
237
238
/* Free a request */
239
void
240
grub_efiemu_mm_return_request (int handle)
241
{
242
  struct grub_efiemu_memrequest *cur, *prev;
243
244
  /* Remove head if necessary */
245
  while (memrequests && memrequests->handle == handle)
246
    {
247
      cur = memrequests->next;
248
      grub_free (memrequests);
249
      memrequests = cur;
250
    }
251
  if (!memrequests)
252
    return;
253
254
  /* Remove request from a middle of chain*/
255
  for (prev = memrequests, cur = prev->next; cur;)
256
    if (cur->handle == handle)
257
      {
258
	prev->next = cur->next;
259
	grub_free (cur);
260
	cur = prev->next;
261
      }
262
    else
263
      {
264
	prev = cur;
265
	cur = prev->next;
266
      }
267
}
268
1792 by Bean
Eliminate nested functions.
269
#ifndef GRUB_UTIL
270
static int
1798 by Bean
Merge with grub r2022.
271
bounds_hook (grub_uint64_t addr __attribute__ ((unused)),
272
	     grub_uint64_t size __attribute__ ((unused)),
273
	     grub_uint32_t type __attribute__ ((unused)),
274
	     void *closure __attribute__ ((unused)))
1792 by Bean
Eliminate nested functions.
275
{
276
  mmap_reserved_size++;
277
  return 0;
278
}
279
#endif
280
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
281
/* Reserve space for memory map */
282
static grub_err_t
283
grub_efiemu_mmap_init (void)
284
{
285
  // the place for memory used by efiemu itself
286
  mmap_reserved_size = GRUB_EFI_MAX_MEMORY_TYPE + 1;
287
288
#ifndef GRUB_UTIL
1792 by Bean
Eliminate nested functions.
289
  grub_machine_mmap_iterate (bounds_hook, 0);
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
290
#endif
291
292
  return GRUB_ERR_NONE;
293
}
294
295
/* This is a drop-in replacement of grub_efi_get_memory_map */
296
/* Get the memory map as defined in the EFI spec. Return 1 if successful,
297
   return 0 if partial, or return -1 if an error occurs.  */
298
int
299
grub_efiemu_get_memory_map (grub_efi_uintn_t *memory_map_size,
300
			    grub_efi_memory_descriptor_t *memory_map,
301
			    grub_efi_uintn_t *map_key,
302
			    grub_efi_uintn_t *descriptor_size,
303
			    grub_efi_uint32_t *descriptor_version)
304
{
305
  if (!efiemu_mmap)
306
    {
1419 by fzielcke
remove all trailing whitespace
307
      grub_error (GRUB_ERR_INVALID_COMMAND,
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
308
		  "you need to first launch efiemu_prepare");
309
      return -1;
310
    }
311
312
  if (*memory_map_size < mmap_num * sizeof (grub_efi_memory_descriptor_t))
313
    {
314
      *memory_map_size = mmap_num * sizeof (grub_efi_memory_descriptor_t);
315
      return 0;
316
    }
317
318
  *memory_map_size = mmap_num * sizeof (grub_efi_memory_descriptor_t);
319
  grub_memcpy (memory_map, efiemu_mmap, *memory_map_size);
320
  if (descriptor_size)
321
    *descriptor_size = sizeof (grub_efi_memory_descriptor_t);
322
  if (descriptor_version)
323
    *descriptor_version = 1;
324
  if (map_key)
325
    *map_key = 0;
326
327
  return 1;
328
}
329
330
/* Free everything */
331
grub_err_t
332
grub_efiemu_mm_unload (void)
333
{
334
  struct grub_efiemu_memrequest *cur, *d;
335
  for (cur = memrequests; cur;)
336
    {
337
      d = cur->next;
338
      grub_free (cur);
339
      cur = d;
340
    }
341
  memrequests = 0;
342
  grub_memset (&requested_memory, 0, sizeof (requested_memory));
343
  grub_free (resident_memory);
344
  resident_memory = 0;
345
  grub_free (efiemu_mmap);
346
  efiemu_mmap = 0;
347
  mmap_reserved_size = mmap_num = 0;
348
  return GRUB_ERR_NONE;
349
}
350
351
/* This function should be called before doing any requests */
352
grub_err_t
353
grub_efiemu_mm_init (void)
354
{
355
  grub_err_t err;
356
357
  err = grub_efiemu_mm_unload ();
358
  if (err)
359
    return err;
360
361
  grub_efiemu_mmap_init ();
362
363
  return GRUB_ERR_NONE;
364
}
365
1792 by Bean
Eliminate nested functions.
366
#ifndef GRUB_UTIL
367
static int
368
fill_hook (grub_uint64_t addr, grub_uint64_t size, grub_uint32_t type,
1798 by Bean
Merge with grub r2022.
369
	   void *closure __attribute__ ((unused)))
1792 by Bean
Eliminate nested functions.
370
{
371
  switch (type)
372
    {
373
    case GRUB_MACHINE_MEMORY_AVAILABLE:
374
      return grub_efiemu_add_to_mmap (addr, size,
375
				      GRUB_EFI_CONVENTIONAL_MEMORY);
376
377
#ifdef GRUB_MACHINE_MEMORY_ACPI
378
    case GRUB_MACHINE_MEMORY_ACPI:
379
      return grub_efiemu_add_to_mmap (addr, size,
380
				      GRUB_EFI_ACPI_RECLAIM_MEMORY);
381
#endif
382
383
#ifdef GRUB_MACHINE_MEMORY_NVS
384
    case GRUB_MACHINE_MEMORY_NVS:
385
      return grub_efiemu_add_to_mmap (addr, size,
386
				      GRUB_EFI_ACPI_MEMORY_NVS);
387
#endif
388
389
    default:
390
      grub_printf ("Unknown memory type %d. Marking as unusable\n", type);
391
    case GRUB_MACHINE_MEMORY_RESERVED:
392
      return grub_efiemu_add_to_mmap (addr, size,
393
				      GRUB_EFI_UNUSABLE_MEMORY);
394
    }
395
}
396
#endif
397
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
398
/* Copy host memory map */
399
static grub_err_t
400
grub_efiemu_mmap_fill (void)
401
{
402
#ifndef GRUB_UTIL
1792 by Bean
Eliminate nested functions.
403
  grub_machine_mmap_iterate (fill_hook, 0);
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
404
#endif
405
406
  return GRUB_ERR_NONE;
407
}
408
409
grub_err_t
1792 by Bean
Eliminate nested functions.
410
grub_efiemu_mmap_iterate (int (*hook) (grub_uint64_t, grub_uint64_t,
411
				       grub_uint32_t, void *), void *closure)
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
412
{
413
  unsigned i;
414
415
  for (i = 0; i < (unsigned) mmap_num; i++)
416
    switch (efiemu_mmap[i].type)
417
      {
418
      case GRUB_EFI_RUNTIME_SERVICES_CODE:
1419 by fzielcke
remove all trailing whitespace
419
	hook (efiemu_mmap[i].physical_start, efiemu_mmap[i].num_pages * 4096,
1792 by Bean
Eliminate nested functions.
420
	      GRUB_EFIEMU_MEMORY_CODE, closure);
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
421
	break;
1419 by fzielcke
remove all trailing whitespace
422
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
423
      case GRUB_EFI_RESERVED_MEMORY_TYPE:
424
      case GRUB_EFI_RUNTIME_SERVICES_DATA:
425
      case GRUB_EFI_UNUSABLE_MEMORY:
426
      case GRUB_EFI_MEMORY_MAPPED_IO:
427
      case GRUB_EFI_MEMORY_MAPPED_IO_PORT_SPACE:
428
      case GRUB_EFI_PAL_CODE:
429
      case GRUB_EFI_MAX_MEMORY_TYPE:
1419 by fzielcke
remove all trailing whitespace
430
	hook (efiemu_mmap[i].physical_start, efiemu_mmap[i].num_pages * 4096,
1792 by Bean
Eliminate nested functions.
431
	      GRUB_EFIEMU_MEMORY_RESERVED, closure);
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
432
	break;
1419 by fzielcke
remove all trailing whitespace
433
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
434
      case GRUB_EFI_LOADER_CODE:
435
      case GRUB_EFI_LOADER_DATA:
436
      case GRUB_EFI_BOOT_SERVICES_CODE:
437
      case GRUB_EFI_BOOT_SERVICES_DATA:
438
      case GRUB_EFI_CONVENTIONAL_MEMORY:
1419 by fzielcke
remove all trailing whitespace
439
	hook (efiemu_mmap[i].physical_start, efiemu_mmap[i].num_pages * 4096,
1792 by Bean
Eliminate nested functions.
440
	      GRUB_EFIEMU_MEMORY_AVAILABLE, closure);
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
441
	break;
1419 by fzielcke
remove all trailing whitespace
442
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
443
      case GRUB_EFI_ACPI_RECLAIM_MEMORY:
1419 by fzielcke
remove all trailing whitespace
444
	hook (efiemu_mmap[i].physical_start, efiemu_mmap[i].num_pages * 4096,
1792 by Bean
Eliminate nested functions.
445
	      GRUB_EFIEMU_MEMORY_ACPI, closure);
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
446
	break;
1419 by fzielcke
remove all trailing whitespace
447
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
448
      case GRUB_EFI_ACPI_MEMORY_NVS:
1419 by fzielcke
remove all trailing whitespace
449
	hook (efiemu_mmap[i].physical_start, efiemu_mmap[i].num_pages * 4096,
1792 by Bean
Eliminate nested functions.
450
	      GRUB_EFIEMU_MEMORY_NVS, closure);
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
451
	break;
452
      }
1419 by fzielcke
remove all trailing whitespace
453
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
454
  return 0;
455
}
456
457
458
/* This function resolves overlapping regions and sorts the memory map
459
   It uses scanline (sweeping) algorithm
460
 */
461
static grub_err_t
462
grub_efiemu_mmap_sort_and_uniq (void)
463
{
1419 by fzielcke
remove all trailing whitespace
464
  /* If same page is used by multiple types it's resolved
465
     according to priority
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
466
     0 - free memory
467
     1 - memory immediately usable after ExitBootServices
468
     2 - memory usable after loading ACPI tables
469
     3 - efiemu memory
470
     4 - unusable memory
471
  */
472
  int priority[GRUB_EFI_MAX_MEMORY_TYPE] =
473
    {
474
      [GRUB_EFI_RESERVED_MEMORY_TYPE] = 4,
475
      [GRUB_EFI_LOADER_CODE] = 1,
476
      [GRUB_EFI_LOADER_DATA] = 1,
477
      [GRUB_EFI_BOOT_SERVICES_CODE] = 1,
478
      [GRUB_EFI_BOOT_SERVICES_DATA] = 1,
479
      [GRUB_EFI_RUNTIME_SERVICES_CODE] = 3,
480
      [GRUB_EFI_RUNTIME_SERVICES_DATA] = 3,
481
      [GRUB_EFI_CONVENTIONAL_MEMORY] = 0,
482
      [GRUB_EFI_UNUSABLE_MEMORY] = 4,
483
      [GRUB_EFI_ACPI_RECLAIM_MEMORY] = 2,
484
      [GRUB_EFI_ACPI_MEMORY_NVS] = 3,
485
      [GRUB_EFI_MEMORY_MAPPED_IO] = 4,
486
      [GRUB_EFI_MEMORY_MAPPED_IO_PORT_SPACE] = 4,
487
      [GRUB_EFI_PAL_CODE] = 4
488
    };
489
490
  int i, j, k, done;
491
492
  /* Scanline events */
493
  struct grub_efiemu_mmap_scan
494
  {
495
    /* At which memory address*/
496
    grub_uint64_t pos;
497
    /* 0 = region starts, 1 = region ends */
498
    int type;
499
    /* Which type of memory region */
500
    grub_efi_memory_type_t memtype;
501
  };
502
  struct grub_efiemu_mmap_scan *scanline_events;
503
  struct grub_efiemu_mmap_scan t;
504
505
  /* Previous scanline event */
506
  grub_uint64_t lastaddr;
507
  int lasttype;
508
  /* Current scanline event */
509
  int curtype;
510
  /* how many regions of given type overlap at current location */
511
  int present[GRUB_EFI_MAX_MEMORY_TYPE];
512
  /* Here is stored the resulting memory map*/
513
  grub_efi_memory_descriptor_t *result;
514
515
  /* Initialize variables*/
516
  grub_memset (present, 0, sizeof (int) * GRUB_EFI_MAX_MEMORY_TYPE);
1419 by fzielcke
remove all trailing whitespace
517
  scanline_events = (struct grub_efiemu_mmap_scan *)
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
518
    grub_malloc (sizeof (struct grub_efiemu_mmap_scan) * 2 * mmap_num);
519
520
  /* Number of chunks can't increase more than by factor of 2 */
1419 by fzielcke
remove all trailing whitespace
521
  result = (grub_efi_memory_descriptor_t *)
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
522
    grub_malloc (sizeof (grub_efi_memory_descriptor_t) * 2 * mmap_num);
523
  if (!result || !scanline_events)
524
    {
525
      grub_free (result);
526
      grub_free (scanline_events);
1419 by fzielcke
remove all trailing whitespace
527
      return grub_error (GRUB_ERR_OUT_OF_MEMORY,
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
528
			 "couldn't allocate space for new memory map");
529
    }
1419 by fzielcke
remove all trailing whitespace
530
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
531
  /* Register scanline events */
532
  for (i = 0; i < mmap_num; i++)
533
    {
534
      scanline_events[2 * i].pos = efiemu_mmap[i].physical_start;
535
      scanline_events[2 * i].type = 0;
536
      scanline_events[2 * i].memtype = efiemu_mmap[i].type;
537
      scanline_events[2 * i + 1].pos = efiemu_mmap[i].physical_start
538
	+ efiemu_mmap[i].num_pages * GRUB_EFIEMU_PAGESIZE;
539
      scanline_events[2 * i + 1].type = 1;
540
      scanline_events[2 * i + 1].memtype = efiemu_mmap[i].type;
541
    }
542
1419 by fzielcke
remove all trailing whitespace
543
  /* Primitive bubble sort. It has complexity O(n^2) but since we're
544
     unlikely to have more than 100 chunks it's probably one of the
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
545
     fastest for one purpose */
546
  done = 1;
547
  while (done)
548
    {
549
      done = 0;
550
      for (i = 0; i < 2 * mmap_num - 1; i++)
551
	if (scanline_events[i + 1].pos < scanline_events[i].pos)
552
	  {
553
	    t = scanline_events[i + 1];
554
	    scanline_events[i + 1] = scanline_events[i];
555
	    scanline_events[i] = t;
556
	    done = 1;
557
	  }
558
    }
559
560
  /* Pointer in resulting memory map */
561
  j = 0;
562
  lastaddr = scanline_events[0].pos;
563
  lasttype = scanline_events[0].memtype;
564
  for (i = 0; i < 2 * mmap_num; i++)
565
    {
566
      /* Process event */
567
      if (scanline_events[i].type)
568
	present[scanline_events[i].memtype]--;
569
      else
570
	present[scanline_events[i].memtype]++;
571
572
      /* Determine current region type */
573
      curtype = -1;
574
      for (k = 0; k < GRUB_EFI_MAX_MEMORY_TYPE; k++)
575
	if (present[k] && (curtype == -1 || priority[k] > priority[curtype]))
576
	  curtype = k;
577
578
      /* Add memory region to resulting map if necessary */
1419 by fzielcke
remove all trailing whitespace
579
      if ((curtype == -1 || curtype != lasttype)
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
580
	  && lastaddr != scanline_events[i].pos
581
	  && lasttype != -1)
582
	{
583
	  result[j].virtual_start = result[j].physical_start = lastaddr;
1419 by fzielcke
remove all trailing whitespace
584
	  result[j].num_pages = (scanline_events[i].pos - lastaddr)
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
585
	    / GRUB_EFIEMU_PAGESIZE;
586
	  result[j].type = lasttype;
587
588
	  /* We set runtime attribute on pages we need to be mapped */
1419 by fzielcke
remove all trailing whitespace
589
	  result[j].attribute
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
590
	    = (lasttype == GRUB_EFI_RUNTIME_SERVICES_CODE
591
		   || lasttype == GRUB_EFI_RUNTIME_SERVICES_DATA)
592
	    ? GRUB_EFI_MEMORY_RUNTIME : 0;
1419 by fzielcke
remove all trailing whitespace
593
	  grub_dprintf ("efiemu",
594
			"mmap entry: type %d start 0x%llx 0x%llx pages\n",
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
595
			result[j].type,
596
			result[j].physical_start, result[j].num_pages);
597
	  j++;
598
	}
599
600
      /* Update last values if necessary */
1419 by fzielcke
remove all trailing whitespace
601
      if (curtype == -1 || curtype != lasttype)
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
602
	{
603
	  lasttype = curtype;
604
	  lastaddr = scanline_events[i].pos;
605
	}
606
    }
607
608
  grub_free (scanline_events);
609
1419 by fzielcke
remove all trailing whitespace
610
  /* Shrink resulting memory map to really used size and replace efiemu_mmap
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
611
     by new value */
612
  grub_free (efiemu_mmap);
613
  efiemu_mmap = grub_realloc (result, j * sizeof (*result));
614
  return GRUB_ERR_NONE;
615
}
616
617
/* This function is called to switch from first to second phase */
618
grub_err_t
619
grub_efiemu_mm_do_alloc (void)
620
{
621
  grub_err_t err;
622
623
  /* Preallocate mmap */
1419 by fzielcke
remove all trailing whitespace
624
  efiemu_mmap = (grub_efi_memory_descriptor_t *)
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
625
    grub_malloc (mmap_reserved_size * sizeof (grub_efi_memory_descriptor_t));
626
  if (!efiemu_mmap)
627
    {
628
      grub_efiemu_unload ();
1760.1.221 by carles
2009-12-24 Carles Pina i Estany <carles@pina.cat>
629
      return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't initialize mmap");
1288 by phcoder
2009-05-02 Vladimir Serbinenko <phcoder@gmail.com>
630
    }
631
632
  if ((err = efiemu_alloc_requests ()))
633
    return err;
634
  if ((err = grub_efiemu_mmap_fill ()))
635
    return err;
636
  return grub_efiemu_mmap_sort_and_uniq ();
1419 by fzielcke
remove all trailing whitespace
637
}