2009-05-02 21:46:34 +00:00
|
|
|
|
/* Mmap management. */
|
|
|
|
|
/*
|
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
|
* Copyright (C) 2009 Free Software Foundation, Inc.
|
|
|
|
|
*
|
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <grub/memory.h>
|
2010-09-04 15:10:10 +00:00
|
|
|
|
#include <grub/machine/memory.h>
|
2009-05-02 21:46:34 +00:00
|
|
|
|
#include <grub/err.h>
|
|
|
|
|
#include <grub/misc.h>
|
|
|
|
|
#include <grub/mm.h>
|
|
|
|
|
#include <grub/command.h>
|
|
|
|
|
#include <grub/dl.h>
|
2010-05-01 18:28:07 +00:00
|
|
|
|
#include <grub/i18n.h>
|
2009-05-02 21:46:34 +00:00
|
|
|
|
|
2011-04-11 21:01:51 +00:00
|
|
|
|
GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
|
|
|
2009-05-02 21:46:34 +00:00
|
|
|
|
#ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
|
|
|
|
|
|
|
|
|
|
struct grub_mmap_region *grub_mmap_overlays = 0;
|
|
|
|
|
static int curhandle = 1;
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-10-14 14:33:44 +00:00
|
|
|
|
static int current_priority = 1;
|
2013-01-15 12:02:35 +00:00
|
|
|
|
|
|
|
|
|
/* Scanline events. */
|
|
|
|
|
struct grub_mmap_scan
|
|
|
|
|
{
|
|
|
|
|
/* At which memory address. */
|
|
|
|
|
grub_uint64_t pos;
|
|
|
|
|
/* 0 = region starts, 1 = region ends. */
|
|
|
|
|
int type;
|
|
|
|
|
/* Which type of memory region? */
|
2013-10-14 14:33:44 +00:00
|
|
|
|
grub_memory_type_t memtype;
|
|
|
|
|
/* Priority. 0 means coming from firmware. */
|
|
|
|
|
int priority;
|
2013-01-15 12:02:35 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Context for grub_mmap_iterate. */
|
|
|
|
|
struct grub_mmap_iterate_ctx
|
|
|
|
|
{
|
|
|
|
|
struct grub_mmap_scan *scanline_events;
|
|
|
|
|
int i;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Helper for grub_mmap_iterate. */
|
|
|
|
|
static int
|
|
|
|
|
count_hook (grub_uint64_t addr __attribute__ ((unused)),
|
|
|
|
|
grub_uint64_t size __attribute__ ((unused)),
|
|
|
|
|
grub_memory_type_t type __attribute__ ((unused)), void *data)
|
|
|
|
|
{
|
|
|
|
|
int *mmap_num = data;
|
|
|
|
|
|
|
|
|
|
(*mmap_num)++;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Helper for grub_mmap_iterate. */
|
|
|
|
|
static int
|
|
|
|
|
fill_hook (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
|
|
|
|
|
void *data)
|
2009-05-02 21:46:34 +00:00
|
|
|
|
{
|
2013-01-15 12:02:35 +00:00
|
|
|
|
struct grub_mmap_iterate_ctx *ctx = data;
|
2009-05-02 21:46:34 +00:00
|
|
|
|
|
2013-10-14 14:33:44 +00:00
|
|
|
|
if (type == GRUB_MEMORY_HOLE)
|
2013-01-15 12:02:35 +00:00
|
|
|
|
{
|
|
|
|
|
grub_dprintf ("mmap", "Unknown memory type %d. Assuming unusable\n",
|
|
|
|
|
type);
|
2013-10-14 14:33:44 +00:00
|
|
|
|
type = GRUB_MEMORY_RESERVED;
|
2013-01-15 12:02:35 +00:00
|
|
|
|
}
|
2013-10-14 14:33:44 +00:00
|
|
|
|
|
|
|
|
|
ctx->scanline_events[ctx->i].pos = addr;
|
|
|
|
|
ctx->scanline_events[ctx->i].type = 0;
|
|
|
|
|
ctx->scanline_events[ctx->i].memtype = type;
|
|
|
|
|
ctx->scanline_events[ctx->i].priority = 0;
|
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
ctx->i++;
|
|
|
|
|
|
|
|
|
|
ctx->scanline_events[ctx->i].pos = addr + size;
|
|
|
|
|
ctx->scanline_events[ctx->i].type = 1;
|
2013-10-14 14:33:44 +00:00
|
|
|
|
ctx->scanline_events[ctx->i].memtype = type;
|
|
|
|
|
ctx->scanline_events[ctx->i].priority = 0;
|
2013-01-15 12:02:35 +00:00
|
|
|
|
ctx->i++;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-14 14:33:44 +00:00
|
|
|
|
struct mm_list
|
|
|
|
|
{
|
|
|
|
|
struct mm_list *next;
|
|
|
|
|
grub_memory_type_t val;
|
|
|
|
|
int present;
|
|
|
|
|
};
|
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
grub_err_t
|
|
|
|
|
grub_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
|
|
|
|
|
{
|
2009-05-02 21:46:34 +00:00
|
|
|
|
/* This function resolves overlapping regions and sorts the memory map.
|
|
|
|
|
It uses scanline (sweeping) algorithm.
|
|
|
|
|
*/
|
2013-01-15 12:02:35 +00:00
|
|
|
|
struct grub_mmap_iterate_ctx ctx;
|
2010-09-04 15:10:10 +00:00
|
|
|
|
int i, done;
|
2009-05-02 21:46:34 +00:00
|
|
|
|
|
|
|
|
|
struct grub_mmap_scan t;
|
|
|
|
|
|
|
|
|
|
/* Previous scanline event. */
|
|
|
|
|
grub_uint64_t lastaddr;
|
|
|
|
|
int lasttype;
|
|
|
|
|
/* Current scanline event. */
|
|
|
|
|
int curtype;
|
2013-10-14 14:33:44 +00:00
|
|
|
|
/* How many regions of given type/priority overlap at current location? */
|
|
|
|
|
/* Normally there shouldn't be more than one region per priority but be robust. */
|
|
|
|
|
struct mm_list *present;
|
2009-05-02 21:46:34 +00:00
|
|
|
|
/* Number of mmap chunks. */
|
|
|
|
|
int mmap_num;
|
|
|
|
|
|
2009-06-10 21:04:23 +00:00
|
|
|
|
#ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
|
2009-05-02 21:46:34 +00:00
|
|
|
|
struct grub_mmap_region *cur;
|
|
|
|
|
#endif
|
2009-06-10 21:04:23 +00:00
|
|
|
|
|
2009-05-02 21:46:34 +00:00
|
|
|
|
mmap_num = 0;
|
|
|
|
|
|
2009-06-10 21:04:23 +00:00
|
|
|
|
#ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
|
2009-05-02 21:46:34 +00:00
|
|
|
|
for (cur = grub_mmap_overlays; cur; cur = cur->next)
|
|
|
|
|
mmap_num++;
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
grub_machine_mmap_iterate (count_hook, &mmap_num);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
|
2009-05-02 21:46:34 +00:00
|
|
|
|
/* Initialize variables. */
|
2013-01-15 12:02:35 +00:00
|
|
|
|
ctx.scanline_events = (struct grub_mmap_scan *)
|
2009-05-02 21:46:34 +00:00
|
|
|
|
grub_malloc (sizeof (struct grub_mmap_scan) * 2 * mmap_num);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
|
2013-10-14 14:33:44 +00:00
|
|
|
|
present = grub_zalloc (sizeof (present[0]) * current_priority);
|
|
|
|
|
|
|
|
|
|
if (! ctx.scanline_events || !present)
|
|
|
|
|
{
|
|
|
|
|
grub_free (ctx.scanline_events);
|
|
|
|
|
grub_free (present);
|
|
|
|
|
return grub_errno;
|
|
|
|
|
}
|
2009-05-02 21:46:34 +00:00
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
ctx.i = 0;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
#ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
|
2009-05-02 21:46:34 +00:00
|
|
|
|
/* Register scanline events. */
|
|
|
|
|
for (cur = grub_mmap_overlays; cur; cur = cur->next)
|
|
|
|
|
{
|
2013-01-15 12:02:35 +00:00
|
|
|
|
ctx.scanline_events[ctx.i].pos = cur->start;
|
|
|
|
|
ctx.scanline_events[ctx.i].type = 0;
|
2013-10-14 14:33:44 +00:00
|
|
|
|
ctx.scanline_events[ctx.i].memtype = cur->type;
|
|
|
|
|
ctx.scanline_events[ctx.i].priority = cur->priority;
|
2013-01-15 12:02:35 +00:00
|
|
|
|
ctx.i++;
|
|
|
|
|
|
|
|
|
|
ctx.scanline_events[ctx.i].pos = cur->end;
|
|
|
|
|
ctx.scanline_events[ctx.i].type = 1;
|
2013-10-14 14:33:44 +00:00
|
|
|
|
ctx.scanline_events[ctx.i].memtype = cur->type;
|
|
|
|
|
ctx.scanline_events[ctx.i].priority = cur->priority;
|
2013-01-15 12:02:35 +00:00
|
|
|
|
ctx.i++;
|
2009-05-02 21:46:34 +00:00
|
|
|
|
}
|
|
|
|
|
#endif /* ! GRUB_MMAP_REGISTER_BY_FIRMWARE */
|
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
grub_machine_mmap_iterate (fill_hook, &ctx);
|
2009-05-02 21:46:34 +00:00
|
|
|
|
|
2009-06-10 21:04:23 +00:00
|
|
|
|
/* Primitive bubble sort. It has complexity O(n^2) but since we're
|
|
|
|
|
unlikely to have more than 100 chunks it's probably one of the
|
2009-05-02 21:46:34 +00:00
|
|
|
|
fastest for one purpose. */
|
|
|
|
|
done = 1;
|
|
|
|
|
while (done)
|
|
|
|
|
{
|
|
|
|
|
done = 0;
|
|
|
|
|
for (i = 0; i < 2 * mmap_num - 1; i++)
|
2013-01-15 12:02:35 +00:00
|
|
|
|
if (ctx.scanline_events[i + 1].pos < ctx.scanline_events[i].pos
|
|
|
|
|
|| (ctx.scanline_events[i + 1].pos == ctx.scanline_events[i].pos
|
|
|
|
|
&& ctx.scanline_events[i + 1].type == 0
|
|
|
|
|
&& ctx.scanline_events[i].type == 1))
|
2009-05-02 21:46:34 +00:00
|
|
|
|
{
|
2013-01-15 12:02:35 +00:00
|
|
|
|
t = ctx.scanline_events[i + 1];
|
|
|
|
|
ctx.scanline_events[i + 1] = ctx.scanline_events[i];
|
|
|
|
|
ctx.scanline_events[i] = t;
|
2009-05-02 21:46:34 +00:00
|
|
|
|
done = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
lastaddr = ctx.scanline_events[0].pos;
|
|
|
|
|
lasttype = ctx.scanline_events[0].memtype;
|
2009-05-02 21:46:34 +00:00
|
|
|
|
for (i = 0; i < 2 * mmap_num; i++)
|
|
|
|
|
{
|
|
|
|
|
/* Process event. */
|
2013-01-15 12:02:35 +00:00
|
|
|
|
if (ctx.scanline_events[i].type)
|
2013-10-14 14:33:44 +00:00
|
|
|
|
{
|
|
|
|
|
if (present[ctx.scanline_events[i].priority].present)
|
|
|
|
|
{
|
|
|
|
|
if (present[ctx.scanline_events[i].priority].val == ctx.scanline_events[i].memtype)
|
|
|
|
|
{
|
|
|
|
|
if (present[ctx.scanline_events[i].priority].next)
|
|
|
|
|
{
|
|
|
|
|
struct mm_list *p = present[ctx.scanline_events[i].priority].next;
|
|
|
|
|
present[ctx.scanline_events[i].priority] = *p;
|
|
|
|
|
grub_free (p);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
present[ctx.scanline_events[i].priority].present = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
struct mm_list **q = &(present[ctx.scanline_events[i].priority].next), *p;
|
|
|
|
|
for (; *q; q = &((*q)->next))
|
|
|
|
|
if ((*q)->val == ctx.scanline_events[i].memtype)
|
|
|
|
|
{
|
|
|
|
|
p = *q;
|
|
|
|
|
*q = p->next;
|
|
|
|
|
grub_free (p);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-05-02 21:46:34 +00:00
|
|
|
|
else
|
2013-10-14 14:33:44 +00:00
|
|
|
|
{
|
|
|
|
|
if (!present[ctx.scanline_events[i].priority].present)
|
|
|
|
|
{
|
|
|
|
|
present[ctx.scanline_events[i].priority].present = 1;
|
|
|
|
|
present[ctx.scanline_events[i].priority].val = ctx.scanline_events[i].memtype;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
struct mm_list *n = grub_malloc (sizeof (*n));
|
|
|
|
|
n->val = ctx.scanline_events[i].memtype;
|
|
|
|
|
n->present = 1;
|
|
|
|
|
n->next = present[ctx.scanline_events[i].priority].next;
|
|
|
|
|
present[ctx.scanline_events[i].priority].next = n;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-05-02 21:46:34 +00:00
|
|
|
|
|
|
|
|
|
/* Determine current region type. */
|
|
|
|
|
curtype = -1;
|
2013-10-14 14:33:44 +00:00
|
|
|
|
{
|
|
|
|
|
int k;
|
|
|
|
|
for (k = current_priority - 1; k >= 0; k--)
|
|
|
|
|
if (present[k].present)
|
|
|
|
|
{
|
|
|
|
|
curtype = present[k].val;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-05-02 21:46:34 +00:00
|
|
|
|
|
2009-05-04 20:06:05 +00:00
|
|
|
|
/* Announce region to the hook if necessary. */
|
2009-06-10 21:04:23 +00:00
|
|
|
|
if ((curtype == -1 || curtype != lasttype)
|
2013-01-15 12:02:35 +00:00
|
|
|
|
&& lastaddr != ctx.scanline_events[i].pos
|
2009-05-02 21:46:34 +00:00
|
|
|
|
&& lasttype != -1
|
2010-09-04 15:10:10 +00:00
|
|
|
|
&& lasttype != GRUB_MEMORY_HOLE
|
2013-01-15 12:02:35 +00:00
|
|
|
|
&& hook (lastaddr, ctx.scanline_events[i].pos - lastaddr, lasttype,
|
|
|
|
|
hook_data))
|
2009-05-02 21:46:34 +00:00
|
|
|
|
{
|
2013-01-15 12:02:35 +00:00
|
|
|
|
grub_free (ctx.scanline_events);
|
2009-05-02 21:46:34 +00:00
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Update last values if necessary. */
|
2009-06-10 21:04:23 +00:00
|
|
|
|
if (curtype == -1 || curtype != lasttype)
|
2009-05-02 21:46:34 +00:00
|
|
|
|
{
|
|
|
|
|
lasttype = curtype;
|
2013-01-15 12:02:35 +00:00
|
|
|
|
lastaddr = ctx.scanline_events[i].pos;
|
2009-05-02 21:46:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
grub_free (ctx.scanline_events);
|
2009-05-02 21:46:34 +00:00
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-10 21:04:23 +00:00
|
|
|
|
#ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
|
2009-05-02 21:46:34 +00:00
|
|
|
|
int
|
|
|
|
|
grub_mmap_register (grub_uint64_t start, grub_uint64_t size, int type)
|
|
|
|
|
{
|
|
|
|
|
struct grub_mmap_region *cur;
|
|
|
|
|
|
|
|
|
|
grub_dprintf ("mmap", "registering\n");
|
|
|
|
|
|
2009-06-10 21:04:23 +00:00
|
|
|
|
cur = (struct grub_mmap_region *)
|
2009-05-02 21:46:34 +00:00
|
|
|
|
grub_malloc (sizeof (struct grub_mmap_region));
|
|
|
|
|
if (! cur)
|
2012-02-08 18:26:01 +00:00
|
|
|
|
return 0;
|
2009-05-02 21:46:34 +00:00
|
|
|
|
|
|
|
|
|
cur->next = grub_mmap_overlays;
|
|
|
|
|
cur->start = start;
|
|
|
|
|
cur->end = start + size;
|
|
|
|
|
cur->type = type;
|
|
|
|
|
cur->handle = curhandle++;
|
2013-10-14 14:33:44 +00:00
|
|
|
|
cur->priority = current_priority++;
|
2009-05-02 21:46:34 +00:00
|
|
|
|
grub_mmap_overlays = cur;
|
|
|
|
|
|
|
|
|
|
if (grub_machine_mmap_register (start, size, type, curhandle))
|
|
|
|
|
{
|
|
|
|
|
grub_mmap_overlays = cur->next;
|
|
|
|
|
grub_free (cur);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cur->handle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
grub_err_t
|
|
|
|
|
grub_mmap_unregister (int handle)
|
|
|
|
|
{
|
|
|
|
|
struct grub_mmap_region *cur, *prev;
|
|
|
|
|
|
2012-02-08 18:26:01 +00:00
|
|
|
|
for (cur = grub_mmap_overlays, prev = 0; cur; prev = cur, cur = cur->next)
|
2009-05-02 21:46:34 +00:00
|
|
|
|
if (handle == cur->handle)
|
|
|
|
|
{
|
|
|
|
|
grub_err_t err;
|
2012-02-08 18:26:01 +00:00
|
|
|
|
err = grub_machine_mmap_unregister (handle);
|
|
|
|
|
if (err)
|
2009-05-02 21:46:34 +00:00
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
if (prev)
|
|
|
|
|
prev->next = cur->next;
|
|
|
|
|
else
|
|
|
|
|
grub_mmap_overlays = cur->next;
|
|
|
|
|
grub_free (cur);
|
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
|
}
|
2012-02-08 18:26:01 +00:00
|
|
|
|
return grub_error (GRUB_ERR_BUG, "mmap overlay not found");
|
2009-05-02 21:46:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* ! GRUB_MMAP_REGISTER_BY_FIRMWARE */
|
|
|
|
|
|
|
|
|
|
#define CHUNK_SIZE 0x400
|
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
struct badram_entry {
|
|
|
|
|
grub_uint64_t addr, mask;
|
|
|
|
|
};
|
|
|
|
|
|
2009-06-10 21:04:23 +00:00
|
|
|
|
static inline grub_uint64_t
|
2013-01-15 12:02:35 +00:00
|
|
|
|
fill_mask (struct badram_entry *entry, grub_uint64_t iterator)
|
2009-05-02 21:46:34 +00:00
|
|
|
|
{
|
|
|
|
|
int i, j;
|
2013-01-15 12:02:35 +00:00
|
|
|
|
grub_uint64_t ret = (entry->addr & entry->mask);
|
2009-05-02 21:46:34 +00:00
|
|
|
|
|
|
|
|
|
/* Find first fixed bit. */
|
|
|
|
|
for (i = 0; i < 64; i++)
|
2013-01-15 12:02:35 +00:00
|
|
|
|
if ((entry->mask & (1ULL << i)) != 0)
|
2009-05-02 21:46:34 +00:00
|
|
|
|
break;
|
|
|
|
|
j = 0;
|
|
|
|
|
for (; i < 64; i++)
|
2013-01-15 12:02:35 +00:00
|
|
|
|
if ((entry->mask & (1ULL << i)) == 0)
|
2009-05-02 21:46:34 +00:00
|
|
|
|
{
|
|
|
|
|
if ((iterator & (1ULL << j)) != 0)
|
|
|
|
|
ret |= 1ULL << i;
|
|
|
|
|
j++;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
/* Helper for grub_cmd_badram. */
|
|
|
|
|
static int
|
|
|
|
|
badram_iter (grub_uint64_t addr, grub_uint64_t size,
|
|
|
|
|
grub_memory_type_t type __attribute__ ((unused)), void *data)
|
|
|
|
|
{
|
|
|
|
|
struct badram_entry *entry = data;
|
|
|
|
|
grub_uint64_t iterator, low, high, cur;
|
|
|
|
|
int tail, var;
|
|
|
|
|
int i;
|
|
|
|
|
grub_dprintf ("badram", "hook %llx+%llx\n", (unsigned long long) addr,
|
|
|
|
|
(unsigned long long) size);
|
|
|
|
|
|
|
|
|
|
/* How many trailing zeros? */
|
|
|
|
|
for (tail = 0; ! (entry->mask & (1ULL << tail)); tail++);
|
|
|
|
|
|
|
|
|
|
/* How many zeros in mask? */
|
|
|
|
|
var = 0;
|
|
|
|
|
for (i = 0; i < 64; i++)
|
|
|
|
|
if (! (entry->mask & (1ULL << i)))
|
|
|
|
|
var++;
|
|
|
|
|
|
|
|
|
|
if (fill_mask (entry, 0) >= addr)
|
|
|
|
|
iterator = 0;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
low = 0;
|
|
|
|
|
high = ~0ULL;
|
|
|
|
|
/* Find starting value. Keep low and high such that
|
|
|
|
|
fill_mask (low) < addr and fill_mask (high) >= addr;
|
|
|
|
|
*/
|
|
|
|
|
while (high - low > 1)
|
|
|
|
|
{
|
|
|
|
|
cur = (low + high) / 2;
|
|
|
|
|
if (fill_mask (entry, cur) >= addr)
|
|
|
|
|
high = cur;
|
|
|
|
|
else
|
|
|
|
|
low = cur;
|
|
|
|
|
}
|
|
|
|
|
iterator = high;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (; iterator < (1ULL << (var - tail))
|
|
|
|
|
&& (cur = fill_mask (entry, iterator)) < addr + size;
|
|
|
|
|
iterator++)
|
|
|
|
|
{
|
|
|
|
|
grub_dprintf ("badram", "%llx (size %llx) is a badram range\n",
|
|
|
|
|
(unsigned long long) cur, (1ULL << tail));
|
|
|
|
|
grub_mmap_register (cur, (1ULL << tail), GRUB_MEMORY_HOLE);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-02 21:46:34 +00:00
|
|
|
|
static grub_err_t
|
|
|
|
|
grub_cmd_badram (grub_command_t cmd __attribute__ ((unused)),
|
|
|
|
|
int argc, char **args)
|
|
|
|
|
{
|
misc: Make grub_strtol() "end" pointers have safer const qualifiers
Currently the string functions grub_strtol(), grub_strtoul(), and
grub_strtoull() don't declare the "end" pointer in such a way as to
require the pointer itself or the character array to be immutable to the
implementation, nor does the C standard do so in its similar functions,
though it does require us not to change any of it.
The typical declarations of these functions follow this pattern:
long
strtol(const char * restrict nptr, char ** restrict endptr, int base);
Much of the reason for this is historic, and a discussion of that
follows below, after the explanation of this change. (GRUB currently
does not include the "restrict" qualifiers, and we name the arguments a
bit differently.)
The implementation is semantically required to treat the character array
as immutable, but such accidental modifications aren't stopped by the
compiler, and the semantics for both the callers and the implementation
of these functions are sometimes also helped by adding that requirement.
This patch changes these declarations to follow this pattern instead:
long
strtol(const char * restrict nptr,
const char ** const restrict endptr,
int base);
This means that if any modification to these functions accidentally
introduces either an errant modification to the underlying character
array, or an accidental assignment to endptr rather than *endptr, the
compiler should generate an error. (The two uses of "restrict" in this
case basically mean strtol() isn't allowed to modify the character array
by going through *endptr, and endptr isn't allowed to point inside the
array.)
It also means the typical use case changes to:
char *s = ...;
const char *end;
long l;
l = strtol(s, &end, 10);
Or even:
const char *p = str;
while (p && *p) {
long l = strtol(p, &p, 10);
...
}
This fixes 26 places where we discard our attempts at treating the data
safely by doing:
const char *p = str;
long l;
l = strtol(p, (char **)&ptr, 10);
It also adds 5 places where we do:
char *p = str;
while (p && *p) {
long l = strtol(p, (const char ** const)&p, 10);
...
/* more calls that need p not to be pointer-to-const */
}
While moderately distasteful, this is a better problem to have.
With one minor exception, I have tested that all of this compiles
without relevant warnings or errors, and that /much/ of it behaves
correctly, with gcc 9 using 'gcc -W -Wall -Wextra'. The one exception
is the changes in grub-core/osdep/aros/hostdisk.c , which I have no idea
how to build.
Because the C standard defined type-qualifiers in a way that can be
confusing, in the past there's been a slow but fairly regular stream of
churn within our patches, which add and remove the const qualifier in many
of the users of these functions. This change should help avoid that in
the future, and in order to help ensure this, I've added an explanation
in misc.h so that when someone does get a compiler warning about a type
error, they have the fix at hand.
The reason we don't have "const" in these calls in the standard is
purely anachronistic: C78 (de facto) did not have type qualifiers in the
syntax, and the "const" type qualifier was added for C89 (I think; it
may have been later). strtol() appears to date from 4.3BSD in 1986,
which means it could not be added to those functions in the standard
without breaking compatibility, which is usually avoided.
The syntax chosen for type qualifiers is what has led to the churn
regarding usage of const, and is especially confusing on string
functions due to the lack of a string type. Quoting from C99, the
syntax is:
declarator:
pointer[opt] direct-declarator
direct-declarator:
identifier
( declarator )
direct-declarator [ type-qualifier-list[opt] assignment-expression[opt] ]
...
direct-declarator [ type-qualifier-list[opt] * ]
...
pointer:
* type-qualifier-list[opt]
* type-qualifier-list[opt] pointer
type-qualifier-list:
type-qualifier
type-qualifier-list type-qualifier
...
type-qualifier:
const
restrict
volatile
So the examples go like:
const char foo; // immutable object
const char *foo; // mutable pointer to object
char * const foo; // immutable pointer to mutable object
const char * const foo; // immutable pointer to immutable object
const char const * const foo; // XXX extra const keyword in the middle
const char * const * const foo; // immutable pointer to immutable
// pointer to immutable object
const char ** const foo; // immutable pointer to mutable pointer
// to immutable object
Making const left-associative for * and right-associative for everything
else may not have been the best choice ever, but here we are, and the
inevitable result is people using trying to use const (as they should!),
putting it at the wrong place, fighting with the compiler for a bit, and
then either removing it or typecasting something in a bad way. I won't
go into describing restrict, but its syntax has exactly the same issue
as with const.
Anyway, the last example above actually represents the *behavior* that's
required of strtol()-like functions, so that's our choice for the "end"
pointer.
Signed-off-by: Peter Jones <pjones@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2020-02-21 21:39:33 +00:00
|
|
|
|
const char *str;
|
2013-01-15 12:02:35 +00:00
|
|
|
|
struct badram_entry entry;
|
2009-05-02 21:46:34 +00:00
|
|
|
|
|
|
|
|
|
if (argc != 1)
|
2012-02-12 14:25:25 +00:00
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
|
2009-05-02 21:46:34 +00:00
|
|
|
|
|
|
|
|
|
grub_dprintf ("badram", "executing badram\n");
|
|
|
|
|
|
|
|
|
|
str = args[0];
|
|
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
/* Parse address and mask. */
|
2013-01-15 12:02:35 +00:00
|
|
|
|
entry.addr = grub_strtoull (str, &str, 16);
|
2009-05-02 21:46:34 +00:00
|
|
|
|
if (*str == ',')
|
|
|
|
|
str++;
|
2013-01-15 12:02:35 +00:00
|
|
|
|
entry.mask = grub_strtoull (str, &str, 16);
|
2009-05-02 21:46:34 +00:00
|
|
|
|
if (*str == ',')
|
|
|
|
|
str++;
|
|
|
|
|
|
|
|
|
|
if (grub_errno == GRUB_ERR_BAD_NUMBER)
|
|
|
|
|
{
|
|
|
|
|
grub_errno = 0;
|
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* When part of a page is tainted, we discard the whole of it. There's
|
|
|
|
|
no point in providing sub-page chunks. */
|
2013-01-15 12:02:35 +00:00
|
|
|
|
entry.mask &= ~(CHUNK_SIZE - 1);
|
2009-05-02 21:46:34 +00:00
|
|
|
|
|
2009-06-10 21:04:23 +00:00
|
|
|
|
grub_dprintf ("badram", "badram %llx:%llx\n",
|
2013-01-15 12:02:35 +00:00
|
|
|
|
(unsigned long long) entry.addr,
|
|
|
|
|
(unsigned long long) entry.mask);
|
2009-05-02 21:46:34 +00:00
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
grub_mmap_iterate (badram_iter, &entry);
|
2009-05-02 21:46:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-20 20:24:30 +00:00
|
|
|
|
static grub_uint64_t
|
|
|
|
|
parsemem (const char *str)
|
|
|
|
|
{
|
|
|
|
|
grub_uint64_t ret;
|
misc: Make grub_strtol() "end" pointers have safer const qualifiers
Currently the string functions grub_strtol(), grub_strtoul(), and
grub_strtoull() don't declare the "end" pointer in such a way as to
require the pointer itself or the character array to be immutable to the
implementation, nor does the C standard do so in its similar functions,
though it does require us not to change any of it.
The typical declarations of these functions follow this pattern:
long
strtol(const char * restrict nptr, char ** restrict endptr, int base);
Much of the reason for this is historic, and a discussion of that
follows below, after the explanation of this change. (GRUB currently
does not include the "restrict" qualifiers, and we name the arguments a
bit differently.)
The implementation is semantically required to treat the character array
as immutable, but such accidental modifications aren't stopped by the
compiler, and the semantics for both the callers and the implementation
of these functions are sometimes also helped by adding that requirement.
This patch changes these declarations to follow this pattern instead:
long
strtol(const char * restrict nptr,
const char ** const restrict endptr,
int base);
This means that if any modification to these functions accidentally
introduces either an errant modification to the underlying character
array, or an accidental assignment to endptr rather than *endptr, the
compiler should generate an error. (The two uses of "restrict" in this
case basically mean strtol() isn't allowed to modify the character array
by going through *endptr, and endptr isn't allowed to point inside the
array.)
It also means the typical use case changes to:
char *s = ...;
const char *end;
long l;
l = strtol(s, &end, 10);
Or even:
const char *p = str;
while (p && *p) {
long l = strtol(p, &p, 10);
...
}
This fixes 26 places where we discard our attempts at treating the data
safely by doing:
const char *p = str;
long l;
l = strtol(p, (char **)&ptr, 10);
It also adds 5 places where we do:
char *p = str;
while (p && *p) {
long l = strtol(p, (const char ** const)&p, 10);
...
/* more calls that need p not to be pointer-to-const */
}
While moderately distasteful, this is a better problem to have.
With one minor exception, I have tested that all of this compiles
without relevant warnings or errors, and that /much/ of it behaves
correctly, with gcc 9 using 'gcc -W -Wall -Wextra'. The one exception
is the changes in grub-core/osdep/aros/hostdisk.c , which I have no idea
how to build.
Because the C standard defined type-qualifiers in a way that can be
confusing, in the past there's been a slow but fairly regular stream of
churn within our patches, which add and remove the const qualifier in many
of the users of these functions. This change should help avoid that in
the future, and in order to help ensure this, I've added an explanation
in misc.h so that when someone does get a compiler warning about a type
error, they have the fix at hand.
The reason we don't have "const" in these calls in the standard is
purely anachronistic: C78 (de facto) did not have type qualifiers in the
syntax, and the "const" type qualifier was added for C89 (I think; it
may have been later). strtol() appears to date from 4.3BSD in 1986,
which means it could not be added to those functions in the standard
without breaking compatibility, which is usually avoided.
The syntax chosen for type qualifiers is what has led to the churn
regarding usage of const, and is especially confusing on string
functions due to the lack of a string type. Quoting from C99, the
syntax is:
declarator:
pointer[opt] direct-declarator
direct-declarator:
identifier
( declarator )
direct-declarator [ type-qualifier-list[opt] assignment-expression[opt] ]
...
direct-declarator [ type-qualifier-list[opt] * ]
...
pointer:
* type-qualifier-list[opt]
* type-qualifier-list[opt] pointer
type-qualifier-list:
type-qualifier
type-qualifier-list type-qualifier
...
type-qualifier:
const
restrict
volatile
So the examples go like:
const char foo; // immutable object
const char *foo; // mutable pointer to object
char * const foo; // immutable pointer to mutable object
const char * const foo; // immutable pointer to immutable object
const char const * const foo; // XXX extra const keyword in the middle
const char * const * const foo; // immutable pointer to immutable
// pointer to immutable object
const char ** const foo; // immutable pointer to mutable pointer
// to immutable object
Making const left-associative for * and right-associative for everything
else may not have been the best choice ever, but here we are, and the
inevitable result is people using trying to use const (as they should!),
putting it at the wrong place, fighting with the compiler for a bit, and
then either removing it or typecasting something in a bad way. I won't
go into describing restrict, but its syntax has exactly the same issue
as with const.
Anyway, the last example above actually represents the *behavior* that's
required of strtol()-like functions, so that's our choice for the "end"
pointer.
Signed-off-by: Peter Jones <pjones@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2020-02-21 21:39:33 +00:00
|
|
|
|
const char *ptr;
|
2010-09-20 20:24:30 +00:00
|
|
|
|
|
|
|
|
|
ret = grub_strtoul (str, &ptr, 0);
|
|
|
|
|
|
|
|
|
|
switch (*ptr)
|
|
|
|
|
{
|
|
|
|
|
case 'K':
|
|
|
|
|
return ret << 10;
|
|
|
|
|
case 'M':
|
|
|
|
|
return ret << 20;
|
|
|
|
|
case 'G':
|
|
|
|
|
return ret << 30;
|
|
|
|
|
case 'T':
|
|
|
|
|
return ret << 40;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
struct cutmem_range {
|
2010-09-20 20:24:30 +00:00
|
|
|
|
grub_uint64_t from, to;
|
2013-01-15 12:02:35 +00:00
|
|
|
|
};
|
2010-09-20 20:11:52 +00:00
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
/* Helper for grub_cmd_cutmem. */
|
|
|
|
|
static int
|
|
|
|
|
cutmem_iter (grub_uint64_t addr, grub_uint64_t size,
|
|
|
|
|
grub_memory_type_t type __attribute__ ((unused)), void *data)
|
|
|
|
|
{
|
|
|
|
|
struct cutmem_range *range = data;
|
|
|
|
|
grub_uint64_t end = addr + size;
|
2010-09-20 20:24:30 +00:00
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
if (addr <= range->from)
|
|
|
|
|
addr = range->from;
|
|
|
|
|
if (end >= range->to)
|
|
|
|
|
end = range->to;
|
2010-09-20 20:11:52 +00:00
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
if (end <= addr)
|
2010-09-20 20:11:52 +00:00
|
|
|
|
return 0;
|
2013-01-15 12:02:35 +00:00
|
|
|
|
|
|
|
|
|
grub_mmap_register (addr, end - addr, GRUB_MEMORY_HOLE);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static grub_err_t
|
|
|
|
|
grub_cmd_cutmem (grub_command_t cmd __attribute__ ((unused)),
|
|
|
|
|
int argc, char **args)
|
|
|
|
|
{
|
|
|
|
|
struct cutmem_range range;
|
2010-09-20 20:11:52 +00:00
|
|
|
|
|
2010-09-20 20:24:30 +00:00
|
|
|
|
if (argc != 2)
|
2012-02-08 18:26:01 +00:00
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
|
2010-09-20 20:11:52 +00:00
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
range.from = parsemem (args[0]);
|
2010-09-20 20:11:52 +00:00
|
|
|
|
if (grub_errno)
|
|
|
|
|
return grub_errno;
|
2010-09-20 20:24:30 +00:00
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
range.to = parsemem (args[1]);
|
2010-09-20 20:24:30 +00:00
|
|
|
|
if (grub_errno)
|
|
|
|
|
return grub_errno;
|
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
grub_mmap_iterate (cutmem_iter, &range);
|
2010-09-20 20:11:52 +00:00
|
|
|
|
|
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static grub_command_t cmd, cmd_cut;
|
2009-05-02 21:46:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GRUB_MOD_INIT(mmap)
|
|
|
|
|
{
|
|
|
|
|
cmd = grub_register_command ("badram", grub_cmd_badram,
|
2010-05-01 18:28:07 +00:00
|
|
|
|
N_("ADDR1,MASK1[,ADDR2,MASK2[,...]]"),
|
2012-02-08 18:26:01 +00:00
|
|
|
|
N_("Declare memory regions as faulty (badram)."));
|
2010-09-20 20:11:52 +00:00
|
|
|
|
cmd_cut = grub_register_command ("cutmem", grub_cmd_cutmem,
|
2010-09-20 20:24:30 +00:00
|
|
|
|
N_("FROM[K|M|G] TO[K|M|G]"),
|
|
|
|
|
N_("Remove any memory regions in specified range."));
|
2010-09-20 20:11:52 +00:00
|
|
|
|
|
2009-05-02 21:46:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GRUB_MOD_FINI(mmap)
|
|
|
|
|
{
|
|
|
|
|
grub_unregister_command (cmd);
|
2010-09-20 20:11:52 +00:00
|
|
|
|
grub_unregister_command (cmd_cut);
|
2009-05-02 21:46:34 +00:00
|
|
|
|
}
|
|
|
|
|
|