~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to mmap/mmap.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Evan Broder, Mario Limonciello
  • Date: 2010-11-24 13:59:55 UTC
  • mfrom: (1.17.6 upstream) (17.6.15 experimental)
  • Revision ID: james.westby@ubuntu.com-20101124135955-r6ii5sepayr7jt53
Tags: 1.99~20101124-1ubuntu1
[ Colin Watson ]
* Resynchronise with Debian experimental.  Remaining changes:
  - Adjust for default Ubuntu boot options ("quiet splash").
  - Default to hiding the menu; holding down Shift at boot will show it.
  - Set a monochromatic theme for Ubuntu.
  - Apply Ubuntu GRUB Legacy changes to legacy update-grub script: title,
    recovery mode, quiet option, tweak how memtest86+ is displayed, and
    use UUIDs where appropriate.
  - Fix backslash-escaping in merge_debconf_into_conf.
  - Remove "GNU/Linux" from default distributor string.
  - Add crashkernel= options if kdump and makedumpfile are available.
  - If other operating systems are installed, then automatically unhide
    the menu.  Otherwise, if GRUB_HIDDEN_TIMEOUT is 0, then use keystatus
    if available to check whether Shift is pressed.  If it is, show the
    menu, otherwise boot immediately.  If keystatus is not available, then
    fall back to a short delay interruptible with Escape.
  - Allow Shift to interrupt 'sleep --interruptible'.
  - Don't display introductory message about line editing unless we're
    actually offering a shell prompt.  Don't clear the screen just before
    booting if we never drew the menu in the first place.
  - Remove some verbose messages printed before reading the configuration
    file.
  - Suppress progress messages as the kernel and initrd load for
    non-recovery kernel menu entries.
  - Change prepare_grub_to_access_device to handle filesystems
    loop-mounted on file images.
  - Ignore devices loop-mounted from files in 10_linux.
  - Show the boot menu if the previous boot failed, that is if it failed
    to get to the end of one of the normal runlevels.
  - Don't generate /boot/grub/device.map during grub-install or
    grub-mkconfig by default.
  - Adjust upgrade version checks for Ubuntu.
  - Don't display "GRUB loading" unless Shift is held down.
  - Adjust versions of grub-doc and grub-legacy-doc conflicts to tolerate
    our backport of the grub-doc split.
  - Fix LVM/RAID probing in the absence of /boot/grub/device.map.
  - Look for .mo files in /usr/share/locale-langpack as well, in
    preference.
  - Make sure GRUB_TIMEOUT isn't quoted unnecessarily.
  - Probe all devices in 'grub-probe --target=drive' if
    /boot/grub/device.map is missing.
  - Build-depend on qemu-kvm rather than qemu-system for grub-pc tests.
  - Use qemu rather than qemu-system-i386.
  - Program vesafb on BIOS systems rather than efifb.
  - Add a grub-rescue-efi-amd64 package containing a rescue CD-ROM image
    for EFI-AMD64.
  - On Wubi, don't ask for an install device, but just update wubildr
    using the diverted grub-install.
  - When embedding the core image in a post-MBR gap, check for and avoid
    sectors matching any of a list of known signatures.
  - Disable video_bochs and video_cirrus on PC BIOS systems, as probing
    PCI space seems to break on some systems.
* Downgrade "ACPI shutdown failed" error to a debug message, since it can
  cause spurious test failures.

[ Evan Broder ]
* Enable lua from grub-extras.
* Incorporate the bitop library into lua.
* Add enum_pci function to grub module in lua.
* Switch back to gfxpayload=keep by default, unless the video hardware
  is known to not support it.

[ Mario Limonciello ]
* Built part_msdos and vfat into bootx64.efi (LP: #677758)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Mmap management. */
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
 
 
20
 
#include <grub/machine/memory.h>
21
 
#include <grub/memory.h>
22
 
#include <grub/err.h>
23
 
#include <grub/misc.h>
24
 
#include <grub/mm.h>
25
 
#include <grub/command.h>
26
 
#include <grub/dl.h>
27
 
#include <grub/i18n.h>
28
 
 
29
 
#ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
30
 
 
31
 
struct grub_mmap_region *grub_mmap_overlays = 0;
32
 
static int curhandle = 1;
33
 
 
34
 
#endif
35
 
 
36
 
grub_err_t
37
 
grub_mmap_iterate (int NESTED_FUNC_ATTR (*hook) (grub_uint64_t,
38
 
                                                 grub_uint64_t, grub_uint32_t))
39
 
{
40
 
 
41
 
  /* This function resolves overlapping regions and sorts the memory map.
42
 
     It uses scanline (sweeping) algorithm.
43
 
  */
44
 
  /* If same page is used by multiple types it's resolved
45
 
     according to priority:
46
 
     1 - free memory
47
 
     2 - memory usable by firmware-aware code
48
 
     3 - unusable memory
49
 
     4 - a range deliberately empty
50
 
  */
51
 
  int priority[GRUB_MACHINE_MEMORY_MAX_TYPE + 2] =
52
 
    {
53
 
#ifdef GRUB_MACHINE_MEMORY_AVAILABLE
54
 
      [GRUB_MACHINE_MEMORY_AVAILABLE] = 1,
55
 
#endif
56
 
#if defined (GRUB_MACHINE_MEMORY_RESERVED) && GRUB_MACHINE_MEMORY_RESERVED != GRUB_MACHINE_MEMORY_HOLE
57
 
      [GRUB_MACHINE_MEMORY_RESERVED] = 3,
58
 
#endif
59
 
#ifdef GRUB_MACHINE_MEMORY_ACPI
60
 
      [GRUB_MACHINE_MEMORY_ACPI] = 2,
61
 
#endif
62
 
#ifdef GRUB_MACHINE_MEMORY_CODE
63
 
      [GRUB_MACHINE_MEMORY_CODE] = 3,
64
 
#endif
65
 
#ifdef GRUB_MACHINE_MEMORY_NVS
66
 
      [GRUB_MACHINE_MEMORY_NVS] = 3,
67
 
#endif
68
 
      [GRUB_MACHINE_MEMORY_HOLE] = 4,
69
 
    };
70
 
 
71
 
  int i, k, done;
72
 
 
73
 
  /* Scanline events. */
74
 
  struct grub_mmap_scan
75
 
  {
76
 
    /* At which memory address. */
77
 
    grub_uint64_t pos;
78
 
    /* 0 = region starts, 1 = region ends. */
79
 
    int type;
80
 
    /* Which type of memory region? */
81
 
    int memtype;
82
 
  };
83
 
  struct grub_mmap_scan *scanline_events;
84
 
  struct grub_mmap_scan t;
85
 
 
86
 
  /* Previous scanline event. */
87
 
  grub_uint64_t lastaddr;
88
 
  int lasttype;
89
 
  /* Current scanline event. */
90
 
  int curtype;
91
 
  /* How many regions of given type overlap at current location? */
92
 
  int present[GRUB_MACHINE_MEMORY_MAX_TYPE + 2];
93
 
  /* Number of mmap chunks. */
94
 
  int mmap_num;
95
 
 
96
 
#ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
97
 
  struct grub_mmap_region *cur;
98
 
#endif
99
 
 
100
 
  auto int NESTED_FUNC_ATTR count_hook (grub_uint64_t, grub_uint64_t,
101
 
                                        grub_uint32_t);
102
 
  int NESTED_FUNC_ATTR count_hook (grub_uint64_t addr __attribute__ ((unused)),
103
 
                                   grub_uint64_t size __attribute__ ((unused)),
104
 
                                   grub_uint32_t type __attribute__ ((unused)))
105
 
  {
106
 
    mmap_num++;
107
 
    return 0;
108
 
  }
109
 
 
110
 
  auto int NESTED_FUNC_ATTR fill_hook (grub_uint64_t, grub_uint64_t,
111
 
                                        grub_uint32_t);
112
 
  int NESTED_FUNC_ATTR fill_hook (grub_uint64_t addr,
113
 
                                  grub_uint64_t size,
114
 
                                  grub_uint32_t type)
115
 
  {
116
 
    scanline_events[i].pos = addr;
117
 
    scanline_events[i].type = 0;
118
 
    if (type <= GRUB_MACHINE_MEMORY_MAX_TYPE && priority[type])
119
 
      scanline_events[i].memtype = type;
120
 
    else
121
 
      {
122
 
        grub_dprintf ("mmap", "Unknown memory type %d. Assuming unusable\n",
123
 
                      type);
124
 
        scanline_events[i].memtype = GRUB_MACHINE_MEMORY_RESERVED;
125
 
      }
126
 
    i++;
127
 
 
128
 
    scanline_events[i].pos = addr + size;
129
 
    scanline_events[i].type = 1;
130
 
    scanline_events[i].memtype = scanline_events[i - 1].memtype;
131
 
    i++;
132
 
 
133
 
    return 0;
134
 
  }
135
 
 
136
 
  mmap_num = 0;
137
 
 
138
 
#ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
139
 
  for (cur = grub_mmap_overlays; cur; cur = cur->next)
140
 
    mmap_num++;
141
 
#endif
142
 
 
143
 
  grub_machine_mmap_iterate (count_hook);
144
 
 
145
 
  /* Initialize variables. */
146
 
  grub_memset (present, 0, sizeof (present));
147
 
  scanline_events = (struct grub_mmap_scan *)
148
 
    grub_malloc (sizeof (struct grub_mmap_scan) * 2 * mmap_num);
149
 
 
150
 
  if (! scanline_events)
151
 
    {
152
 
      return grub_error (GRUB_ERR_OUT_OF_MEMORY,
153
 
                         "couldn't allocate space for new memory map");
154
 
    }
155
 
 
156
 
  i = 0;
157
 
#ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
158
 
  /* Register scanline events. */
159
 
  for (cur = grub_mmap_overlays; cur; cur = cur->next)
160
 
    {
161
 
      scanline_events[i].pos = cur->start;
162
 
      scanline_events[i].type = 0;
163
 
      if (cur->type == GRUB_MACHINE_MEMORY_HOLE
164
 
          || (cur->type >= 0 && cur->type <= GRUB_MACHINE_MEMORY_MAX_TYPE
165
 
              && priority[cur->type]))
166
 
        scanline_events[i].memtype = cur->type;
167
 
      else
168
 
        scanline_events[i].memtype = GRUB_MACHINE_MEMORY_RESERVED;
169
 
      i++;
170
 
 
171
 
      scanline_events[i].pos = cur->end;
172
 
      scanline_events[i].type = 1;
173
 
      scanline_events[i].memtype = scanline_events[i - 1].memtype;
174
 
      i++;
175
 
    }
176
 
#endif /* ! GRUB_MMAP_REGISTER_BY_FIRMWARE */
177
 
 
178
 
  grub_machine_mmap_iterate (fill_hook);
179
 
 
180
 
  /* Primitive bubble sort. It has complexity O(n^2) but since we're
181
 
     unlikely to have more than 100 chunks it's probably one of the
182
 
     fastest for one purpose. */
183
 
  done = 1;
184
 
  while (done)
185
 
    {
186
 
      done = 0;
187
 
      for (i = 0; i < 2 * mmap_num - 1; i++)
188
 
        if (scanline_events[i + 1].pos < scanline_events[i].pos
189
 
            || (scanline_events[i + 1].pos == scanline_events[i].pos
190
 
                && scanline_events[i + 1].type == 0
191
 
                && scanline_events[i].type == 1))
192
 
          {
193
 
            t = scanline_events[i + 1];
194
 
            scanline_events[i + 1] = scanline_events[i];
195
 
            scanline_events[i] = t;
196
 
            done = 1;
197
 
          }
198
 
    }
199
 
 
200
 
  lastaddr = scanline_events[0].pos;
201
 
  lasttype = scanline_events[0].memtype;
202
 
  for (i = 0; i < 2 * mmap_num; i++)
203
 
    {
204
 
      /* Process event. */
205
 
      if (scanline_events[i].type)
206
 
        present[scanline_events[i].memtype]--;
207
 
      else
208
 
        present[scanline_events[i].memtype]++;
209
 
 
210
 
      /* Determine current region type. */
211
 
      curtype = -1;
212
 
      for (k = 0; k <= GRUB_MACHINE_MEMORY_MAX_TYPE + 1; k++)
213
 
        if (present[k] && (curtype == -1 || priority[k] > priority[curtype]))
214
 
          curtype = k;
215
 
 
216
 
      /* Announce region to the hook if necessary. */
217
 
      if ((curtype == -1 || curtype != lasttype)
218
 
          && lastaddr != scanline_events[i].pos
219
 
          && lasttype != -1
220
 
          && lasttype != GRUB_MACHINE_MEMORY_HOLE
221
 
          && hook (lastaddr, scanline_events[i].pos - lastaddr, lasttype))
222
 
        {
223
 
          grub_free (scanline_events);
224
 
          return GRUB_ERR_NONE;
225
 
        }
226
 
 
227
 
      /* Update last values if necessary. */
228
 
      if (curtype == -1 || curtype != lasttype)
229
 
        {
230
 
          lasttype = curtype;
231
 
          lastaddr = scanline_events[i].pos;
232
 
        }
233
 
    }
234
 
 
235
 
  grub_free (scanline_events);
236
 
  return GRUB_ERR_NONE;
237
 
}
238
 
 
239
 
#ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
240
 
int
241
 
grub_mmap_register (grub_uint64_t start, grub_uint64_t size, int type)
242
 
{
243
 
  struct grub_mmap_region *cur;
244
 
 
245
 
  grub_dprintf ("mmap", "registering\n");
246
 
 
247
 
  cur = (struct grub_mmap_region *)
248
 
    grub_malloc (sizeof (struct grub_mmap_region));
249
 
  if (! cur)
250
 
    {
251
 
      grub_error (GRUB_ERR_OUT_OF_MEMORY,
252
 
                  "couldn't allocate memory map overlay");
253
 
      return 0;
254
 
    }
255
 
 
256
 
  cur->next = grub_mmap_overlays;
257
 
  cur->start = start;
258
 
  cur->end = start + size;
259
 
  cur->type = type;
260
 
  cur->handle = curhandle++;
261
 
  grub_mmap_overlays = cur;
262
 
 
263
 
  if (grub_machine_mmap_register (start, size, type, curhandle))
264
 
    {
265
 
      grub_mmap_overlays = cur->next;
266
 
      grub_free (cur);
267
 
      return 0;
268
 
    }
269
 
 
270
 
  return cur->handle;
271
 
}
272
 
 
273
 
grub_err_t
274
 
grub_mmap_unregister (int handle)
275
 
{
276
 
  struct grub_mmap_region *cur, *prev;
277
 
 
278
 
  for (cur = grub_mmap_overlays, prev = 0; cur; prev= cur, cur = cur->next)
279
 
    if (handle == cur->handle)
280
 
      {
281
 
        grub_err_t err;
282
 
        if ((err = grub_machine_mmap_unregister (handle)))
283
 
          return err;
284
 
 
285
 
        if (prev)
286
 
          prev->next = cur->next;
287
 
        else
288
 
          grub_mmap_overlays = cur->next;
289
 
        grub_free (cur);
290
 
        return GRUB_ERR_NONE;
291
 
      }
292
 
  return grub_error (GRUB_ERR_BAD_ARGUMENT, "mmap overlay not found");
293
 
}
294
 
 
295
 
#endif /* ! GRUB_MMAP_REGISTER_BY_FIRMWARE */
296
 
 
297
 
#define CHUNK_SIZE      0x400
298
 
 
299
 
static inline grub_uint64_t
300
 
fill_mask (grub_uint64_t addr, grub_uint64_t mask, grub_uint64_t iterator)
301
 
{
302
 
  int i, j;
303
 
  grub_uint64_t ret = (addr & mask);
304
 
 
305
 
  /* Find first fixed bit. */
306
 
  for (i = 0; i < 64; i++)
307
 
    if ((mask & (1ULL << i)) != 0)
308
 
      break;
309
 
  j = 0;
310
 
  for (; i < 64; i++)
311
 
    if ((mask & (1ULL << i)) == 0)
312
 
      {
313
 
        if ((iterator & (1ULL << j)) != 0)
314
 
          ret |= 1ULL << i;
315
 
        j++;
316
 
      }
317
 
  return ret;
318
 
}
319
 
 
320
 
static grub_err_t
321
 
grub_cmd_badram (grub_command_t cmd __attribute__ ((unused)),
322
 
                 int argc, char **args)
323
 
{
324
 
  char * str;
325
 
  grub_uint64_t badaddr, badmask;
326
 
 
327
 
  auto int NESTED_FUNC_ATTR hook (grub_uint64_t, grub_uint64_t, grub_uint32_t);
328
 
  int NESTED_FUNC_ATTR hook (grub_uint64_t addr,
329
 
                             grub_uint64_t size,
330
 
                             grub_uint32_t type __attribute__ ((unused)))
331
 
  {
332
 
    grub_uint64_t iterator, low, high, cur;
333
 
    int tail, var;
334
 
    int i;
335
 
    grub_dprintf ("badram", "hook %llx+%llx\n", (unsigned long long) addr,
336
 
                  (unsigned long long) size);
337
 
 
338
 
    /* How many trailing zeros? */
339
 
    for (tail = 0; ! (badmask & (1ULL << tail)); tail++);
340
 
 
341
 
    /* How many zeros in mask? */
342
 
    var = 0;
343
 
    for (i = 0; i < 64; i++)
344
 
      if (! (badmask & (1ULL << i)))
345
 
        var++;
346
 
 
347
 
    if (fill_mask (badaddr, badmask, 0) >= addr)
348
 
      iterator = 0;
349
 
    else
350
 
      {
351
 
        low = 0;
352
 
        high = ~0ULL;
353
 
        /* Find starting value. Keep low and high such that
354
 
           fill_mask (low) < addr and fill_mask (high) >= addr;
355
 
        */
356
 
        while (high - low > 1)
357
 
          {
358
 
            cur = (low + high) / 2;
359
 
            if (fill_mask (badaddr, badmask, cur) >= addr)
360
 
              high = cur;
361
 
            else
362
 
              low = cur;
363
 
          }
364
 
        iterator = high;
365
 
      }
366
 
 
367
 
    for (; iterator < (1ULL << (var - tail))
368
 
           && (cur = fill_mask (badaddr, badmask, iterator)) < addr + size;
369
 
         iterator++)
370
 
      {
371
 
        grub_dprintf ("badram", "%llx (size %llx) is a badram range\n",
372
 
                      (unsigned long long) cur, (1ULL << tail));
373
 
        grub_mmap_register (cur, (1ULL << tail), GRUB_MACHINE_MEMORY_HOLE);
374
 
      }
375
 
    return 0;
376
 
  }
377
 
 
378
 
  if (argc != 1)
379
 
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "badram string required");
380
 
 
381
 
  grub_dprintf ("badram", "executing badram\n");
382
 
 
383
 
  str = args[0];
384
 
 
385
 
  while (1)
386
 
    {
387
 
      /* Parse address and mask.  */
388
 
      badaddr = grub_strtoull (str, &str, 16);
389
 
      if (*str == ',')
390
 
        str++;
391
 
      badmask = grub_strtoull (str, &str, 16);
392
 
      if (*str == ',')
393
 
        str++;
394
 
 
395
 
      if (grub_errno == GRUB_ERR_BAD_NUMBER)
396
 
        {
397
 
          grub_errno = 0;
398
 
          return GRUB_ERR_NONE;
399
 
        }
400
 
 
401
 
      /* When part of a page is tainted, we discard the whole of it.  There's
402
 
         no point in providing sub-page chunks.  */
403
 
      badmask &= ~(CHUNK_SIZE - 1);
404
 
 
405
 
      grub_dprintf ("badram", "badram %llx:%llx\n",
406
 
                    (unsigned long long) badaddr, (unsigned long long) badmask);
407
 
 
408
 
      grub_mmap_iterate (hook);
409
 
    }
410
 
}
411
 
 
412
 
static grub_command_t cmd;
413
 
 
414
 
 
415
 
GRUB_MOD_INIT(mmap)
416
 
{
417
 
  cmd = grub_register_command ("badram", grub_cmd_badram,
418
 
                               N_("ADDR1,MASK1[,ADDR2,MASK2[,...]]"),
419
 
                               N_("Declare memory regions as badram."));
420
 
}
421
 
 
422
 
GRUB_MOD_FINI(mmap)
423
 
{
424
 
  grub_unregister_command (cmd);
425
 
}
426