grub/lib/mips/relocator.c

146 lines
4.1 KiB
C
Raw Normal View History

2009-10-11 21:00:22 +00:00
/*
* 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/mm.h>
#include <grub/misc.h>
#include <grub/types.h>
#include <grub/types.h>
#include <grub/err.h>
#include <grub/cache.h>
2009-10-11 21:00:22 +00:00
#include <grub/mips/relocator.h>
2010-01-23 12:30:24 +00:00
#include <grub/relocator_private.h>
2009-10-11 21:00:22 +00:00
2010-01-23 12:30:24 +00:00
/* Do we need mips64? */
2009-10-11 21:00:22 +00:00
2010-01-23 12:30:24 +00:00
extern grub_uint8_t grub_relocator_forward_start;
extern grub_uint8_t grub_relocator_forward_end;
extern grub_uint8_t grub_relocator_backward_start;
extern grub_uint8_t grub_relocator_backward_end;
2009-10-11 21:00:22 +00:00
#define REGW_SIZEOF (2 * sizeof (grub_uint32_t))
2009-11-29 11:26:15 +00:00
#define JUMP_SIZEOF (2 * sizeof (grub_uint32_t))
2009-10-11 21:00:22 +00:00
2010-01-23 12:30:24 +00:00
#define RELOCATOR_SRC_SIZEOF(x) (&grub_relocator_##x##_end \
- &grub_relocator_##x##_start)
2009-10-11 21:00:22 +00:00
#define RELOCATOR_SIZEOF(x) (RELOCATOR_SRC_SIZEOF(x) \
2010-01-23 12:30:24 +00:00
+ REGW_SIZEOF * 3)
grub_size_t grub_relocator_align = sizeof (grub_uint32_t);
grub_size_t grub_relocator_forward_size;
grub_size_t grub_relocator_backward_size;
grub_size_t grub_relocator_jumper_size = JUMP_SIZEOF + REGW_SIZEOF;
2009-10-11 21:00:22 +00:00
2010-01-23 12:30:24 +00:00
void
grub_cpu_relocator_init (void)
{
grub_relocator_forward_size = RELOCATOR_SIZEOF(forward);
grub_relocator_backward_size = RELOCATOR_SIZEOF(backward);
}
2009-10-11 21:00:22 +00:00
static void
write_reg (int regn, grub_uint32_t val, void **target)
{
/* lui $r, (val+0x8000). */
*(grub_uint32_t *) *target = ((0x3c00 | regn) << 16) | ((val + 0x8000) >> 16);
*target = ((grub_uint32_t *) *target) + 1;
/* addiu $r, $r, val. */
*(grub_uint32_t *) *target = (((0x2400 | regn << 5 | regn) << 16)
2009-10-17 10:18:39 +00:00
| (val & 0xffff));
2009-10-11 21:00:22 +00:00
*target = ((grub_uint32_t *) *target) + 1;
}
static void
write_jump (int regn, void **target)
{
/* j $r. */
*(grub_uint32_t *) *target = (regn<<21) | 0x8;
*target = ((grub_uint32_t *) *target) + 1;
/* nop. */
*(grub_uint32_t *) *target = 0;
*target = ((grub_uint32_t *) *target) + 1;
2009-10-11 21:00:22 +00:00
}
2010-01-23 12:30:24 +00:00
void
grub_cpu_relocator_jumper (void *rels, grub_addr_t addr)
{
write_reg (1, addr, &rels);
write_jump (1, &rels);
}
void
grub_cpu_relocator_backward (void *ptr0, void *src, void *dest,
grub_size_t size)
2009-10-11 21:00:22 +00:00
{
void *ptr = ptr0;
2009-10-19 10:59:33 +00:00
write_reg (8, (grub_uint32_t) src, &ptr);
2010-01-23 12:30:24 +00:00
write_reg (9, (grub_uint32_t) dest, &ptr);
write_reg (10, (grub_uint32_t) size, &ptr);
grub_memcpy (ptr, &grub_relocator_backward_start,
2009-10-11 21:00:22 +00:00
RELOCATOR_SRC_SIZEOF (backward));
}
2010-01-23 12:30:24 +00:00
void
grub_cpu_relocator_forward (void *ptr0, void *src, void *dest,
grub_size_t size)
2009-10-11 21:00:22 +00:00
{
void *ptr = ptr0;
2009-10-19 10:59:33 +00:00
write_reg (8, (grub_uint32_t) src, &ptr);
2010-01-23 12:30:24 +00:00
write_reg (9, (grub_uint32_t) dest, &ptr);
write_reg (10, (grub_uint32_t) size, &ptr);
grub_memcpy (ptr, &grub_relocator_forward_start,
2009-10-11 21:00:22 +00:00
RELOCATOR_SRC_SIZEOF (forward));
2010-01-23 12:30:24 +00:00
}
grub_err_t
grub_relocator32_boot (struct grub_relocator *rel,
struct grub_relocator32_state state)
{
grub_addr_t target;
void *src, *ptr;
grub_err_t err;
grub_addr_t relst;
grub_size_t relsize;
grub_size_t stateset_size = 31 * REGW_SIZEOF + JUMP_SIZEOF;
unsigned i;
err = grub_relocator_alloc_chunk_align (rel, &src, &target, 0,
(0xffffffff - stateset_size)
+ 1, stateset_size,
sizeof (grub_uint32_t),
GRUB_RELOCATOR_PREFERENCE_NONE);
if (err)
return err;
ptr = src;
2009-10-11 21:00:22 +00:00
for (i = 1; i < 32; i++)
write_reg (i, state.gpr[i], &ptr);
write_jump (state.jumpreg, &ptr);
2010-01-23 12:30:24 +00:00
err = grub_relocator_prepare_relocs (rel, target, &relst, &relsize);
if (err)
return err;
grub_arch_sync_caches ((void *) relst, relsize);
((void (*) (void)) relst) ();
/* Not reached. */
return GRUB_ERR_NONE;
}