From 67167531f0235ad70cd31ec2c6d5c1100a48e69f Mon Sep 17 00:00:00 2001 From: Nathan Whitehorn Date: Mon, 12 Jul 2010 00:49:22 +0000 Subject: [PATCH] Provide support in loader for booting 64-bit PowerPC kernels. Like amd64, 64-bit PowerPC kernels are loaded by a 32-bit loader, since nearly all powerpc64 firmwares execute in 32-bit mode. --- sys/boot/Makefile | 6 +- sys/boot/common/Makefile.inc | 5 +- sys/boot/fdt/Makefile | 6 +- sys/boot/ficl/Makefile | 10 ++- sys/boot/ofw/Makefile.inc | 5 ++ sys/boot/ofw/libofw/Makefile | 3 +- sys/boot/ofw/libofw/libofw.h | 3 + sys/boot/ofw/libofw/ppc64_elf_freebsd.c | 101 ++++++++++++++++++++++++ sys/boot/powerpc/Makefile.inc | 5 ++ sys/boot/powerpc/boot1.chrp/Makefile | 2 +- sys/boot/powerpc/ofw/Makefile | 5 +- sys/boot/powerpc/ofw/conf.c | 1 + sys/boot/powerpc/ofw/ldscript.powerpc | 2 +- sys/boot/powerpc/ofw/metadata.c | 68 ++++++++++++---- sys/boot/powerpc/uboot/Makefile | 5 +- sys/boot/powerpc/uboot/ldscript.powerpc | 2 +- sys/boot/uboot/Makefile.inc | 5 ++ 17 files changed, 202 insertions(+), 32 deletions(-) create mode 100644 sys/boot/ofw/libofw/ppc64_elf_freebsd.c diff --git a/sys/boot/Makefile b/sys/boot/Makefile index 751a93f34d50..b2eeb9554703 100644 --- a/sys/boot/Makefile +++ b/sys/boot/Makefile @@ -13,12 +13,12 @@ SUBDIR+= efi .endif # Build Open Firmware library. -.if ${MACHINE_ARCH} == "powerpc" || ${MACHINE_ARCH} == "sparc64" +.if ${MACHINE_ARCH} == "powerpc" || ${MACHINE_ARCH} == "powerpc64" || ${MACHINE_ARCH} == "sparc64" SUBDIR+= ofw .endif # Build U-Boot library. -.if ${MACHINE_ARCH} == "powerpc" || ${MACHINE_ARCH} == "arm" +.if ${MACHINE_ARCH} == "powerpc" || ${MACHINE_ARCH} == "powerpc64" || ${MACHINE_ARCH} == "arm" SUBDIR+= uboot .endif @@ -31,7 +31,7 @@ SUBDIR+= fdt .endif # Pick the machine-dependent subdir based on the target architecture. -ADIR= ${MACHINE:S/amd64/i386/:S/sun4v/sparc64/} +ADIR= ${MACHINE:S/amd64/i386/:S/sun4v/sparc64/:S/powerpc64/powerpc/} .if exists(${.CURDIR}/${ADIR}/.) SUBDIR+= ${ADIR} .endif diff --git a/sys/boot/common/Makefile.inc b/sys/boot/common/Makefile.inc index bb0d12098601..6b4c769ce84b 100644 --- a/sys/boot/common/Makefile.inc +++ b/sys/boot/common/Makefile.inc @@ -9,8 +9,11 @@ SRCS+= load_elf32.c load_elf32_obj.c reloc_elf32.c SRCS+= load_elf64.c load_elf64_obj.c reloc_elf64.c .elif ${MACHINE} == "pc98" SRCS+= load_elf32.c load_elf32_obj.c reloc_elf32.c -.elif ${MACHINE_ARCH} == "powerpc" || ${MACHINE_ARCH} == "arm" +.elif ${MACHINE_ARCH} == "arm" SRCS+= load_elf32.c reloc_elf32.c +.elif ${MACHINE_ARCH} == "powerpc" || ${MACHINE_ARCH} == "powerpc64" +SRCS+= load_elf32.c reloc_elf32.c +SRCS+= load_elf64.c reloc_elf64.c .elif ${MACHINE_ARCH} == "sparc64" || ${MACHINE_ARCH} == "ia64" SRCS+= load_elf64.c reloc_elf64.c .endif diff --git a/sys/boot/fdt/Makefile b/sys/boot/fdt/Makefile index 32ebce8b4d4a..5df761d824b5 100644 --- a/sys/boot/fdt/Makefile +++ b/sys/boot/fdt/Makefile @@ -16,10 +16,14 @@ CFLAGS+= -I${.CURDIR}/../../contrib/libfdt/ -I${.CURDIR}/../common/ \ CFLAGS+= -ffreestanding -.if ${MACHINE_ARCH} == "powerpc" || ${MACHINE_ARCH} == "arm" +.if ${MACHINE_CPUARCH} == "powerpc" || ${MACHINE_CPUARCH} == "arm" CFLAGS+= -msoft-float .endif +.if ${MACHINE_ARCH} == "powerpc64" +CFLAGS+= -m32 +.endif + CFLAGS+= -Wformat -Wall .include diff --git a/sys/boot/ficl/Makefile b/sys/boot/ficl/Makefile index a967613ef1a3..cdc8f7eddeed 100644 --- a/sys/boot/ficl/Makefile +++ b/sys/boot/ficl/Makefile @@ -1,6 +1,6 @@ # $FreeBSD$ # -.PATH: ${.CURDIR}/${MACHINE_ARCH:S/amd64/i386/} +.PATH: ${.CURDIR}/${MACHINE_ARCH:S/amd64/i386/:S/powerpc64/powerpc/} BASE_SRCS= dict.c ficl.c fileaccess.c float.c loader.c math64.c \ prefix.c search.c stack.c tools.c vm.c words.c @@ -14,7 +14,7 @@ CFLAGS+= -mno-mmx -mno-3dnow -mno-sse -mno-sse2 .if ${MACHINE_ARCH} == "i386" CFLAGS+= -mno-sse3 .endif -.if ${MACHINE_ARCH} == "powerpc" || ${MACHINE_ARCH} == "arm" +.if ${MACHINE_ARCH} == "powerpc" || ${MACHINE_ARCH} == "powerpc64" || ${MACHINE_ARCH} == "arm" CFLAGS+= -msoft-float .endif .if ${MACHINE} == "pc98" @@ -45,7 +45,11 @@ SOFTWORDS= softcore.fr jhlocal.fr marker.fr freebsd.fr ficllocal.fr \ CFLAGS+= -m32 -march=i386 -I. .endif -CFLAGS+= -I${.CURDIR} -I${.CURDIR}/${MACHINE_ARCH:S/amd64/i386/} \ +.if ${MACHINE_ARCH} == "powerpc64" +CFLAGS+= -m32 -mcpu=powerpc -I. +.endif + +CFLAGS+= -I${.CURDIR} -I${.CURDIR}/${MACHINE_ARCH:S/amd64/i386/:S/powerpc64/powerpc/} \ -I${.CURDIR}/../common softcore.c: ${SOFTWORDS} softcore.awk diff --git a/sys/boot/ofw/Makefile.inc b/sys/boot/ofw/Makefile.inc index 265f86d1ed55..dab65e52621d 100644 --- a/sys/boot/ofw/Makefile.inc +++ b/sys/boot/ofw/Makefile.inc @@ -1,3 +1,8 @@ # $FreeBSD$ +.if ${MACHINE_ARCH} == "powerpc64" +CFLAGS+= -m32 -mcpu=powerpc +LDFLAGS+= -m elf32ppc +.endif + .include "../Makefile.inc" diff --git a/sys/boot/ofw/libofw/Makefile b/sys/boot/ofw/libofw/Makefile index 8a56a778f76f..5ea0de972af3 100644 --- a/sys/boot/ofw/libofw/Makefile +++ b/sys/boot/ofw/libofw/Makefile @@ -13,8 +13,9 @@ CFLAGS+= -I${.CURDIR}/../../../../lib/libstand/ CFLAGS+= -I${.CURDIR}/../../common -I${.CURDIR}/../../.. -I. CFLAGS+= -ffreestanding -.if ${MACHINE_ARCH} == "powerpc" +.if ${MACHINE_ARCH} == "powerpc" || ${MACHINE_ARCH} == "powerpc64" CFLAGS+= -msoft-float +SRCS+= ppc64_elf_freebsd.c .endif .ifdef(BOOT_DISK_DEBUG) diff --git a/sys/boot/ofw/libofw/libofw.h b/sys/boot/ofw/libofw/libofw.h index 748233e6fe9e..5f6d7ad5e6bd 100644 --- a/sys/boot/ofw/libofw/libofw.h +++ b/sys/boot/ofw/libofw/libofw.h @@ -62,6 +62,9 @@ int ofw_elf_loadfile(char *, vm_offset_t, struct preloaded_file **); int ofw_elf_exec(struct preloaded_file *); extern struct file_format ofw_elf; +#ifdef __powerpc__ +extern struct file_format ofw_elf64; +#endif extern void reboot(void); diff --git a/sys/boot/ofw/libofw/ppc64_elf_freebsd.c b/sys/boot/ofw/libofw/ppc64_elf_freebsd.c new file mode 100644 index 000000000000..84ea40611844 --- /dev/null +++ b/sys/boot/ofw/libofw/ppc64_elf_freebsd.c @@ -0,0 +1,101 @@ +/*- + * Copyright (c) 2001 Benno Rice + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#define __ELF_WORD_SIZE 64 + +#include +#include + +#include +#include + +#include + +#include "bootstrap.h" +#include "libofw.h" +#include "openfirm.h" + +extern char end[]; +extern vm_offset_t reloc; /* From /conf.c */ + +int +ppc64_ofw_elf_loadfile(char *filename, u_int64_t dest, + struct preloaded_file **result) +{ + int r; + + r = __elfN(loadfile)(filename, dest, result); + if (r != 0) + return (r); + + /* + * No need to sync the icache for modules: this will + * be done by the kernel after relocation. + */ + if (!strcmp((*result)->f_type, "elf kernel")) + __syncicache((void *) (*result)->f_addr, (*result)->f_size); + return (0); +} + +int +ppc64_ofw_elf_exec(struct preloaded_file *fp) +{ + struct file_metadata *fmp; + vm_offset_t mdp; + Elf_Ehdr *e; + int error; + intptr_t entry; + + if ((fmp = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL) { + return(EFTYPE); + } + e = (Elf_Ehdr *)&fmp->md_data; + + /* Handle function descriptor */ + entry = *(uint64_t *)e->e_entry; + + if ((error = md_load64(fp->f_args, &mdp)) != 0) + return (error); + + printf("Kernel entry at 0x%lx ...\n", entry); + + dev_cleanup(); + ofw_release_heap(); + + OF_chain((void *)reloc, end - (char *)reloc, (void *)entry, + (void *)mdp, sizeof(mdp)); + + panic("exec returned"); +} + +struct file_format ofw_elf64 = +{ + ppc64_ofw_elf_loadfile, + ppc64_ofw_elf_exec +}; diff --git a/sys/boot/powerpc/Makefile.inc b/sys/boot/powerpc/Makefile.inc index 265f86d1ed55..dab65e52621d 100644 --- a/sys/boot/powerpc/Makefile.inc +++ b/sys/boot/powerpc/Makefile.inc @@ -1,3 +1,8 @@ # $FreeBSD$ +.if ${MACHINE_ARCH} == "powerpc64" +CFLAGS+= -m32 -mcpu=powerpc +LDFLAGS+= -m elf32ppc +.endif + .include "../Makefile.inc" diff --git a/sys/boot/powerpc/boot1.chrp/Makefile b/sys/boot/powerpc/boot1.chrp/Makefile index 621b973ff7aa..f3f7e4dd32b2 100644 --- a/sys/boot/powerpc/boot1.chrp/Makefile +++ b/sys/boot/powerpc/boot1.chrp/Makefile @@ -13,7 +13,7 @@ SRCS= boot1.c ashldi3.c INTERNALPROG= NO_MAN= -CFLAGS= -ffreestanding -msoft-float -Os -D_KERNEL \ +CFLAGS= -ffreestanding -msoft-float -Os \ -I${.CURDIR}/../../common -I${.CURDIR}/../../../ LDFLAGS=-nostdlib -static -N diff --git a/sys/boot/powerpc/ofw/Makefile b/sys/boot/powerpc/ofw/Makefile index d42fa927ce5c..776f98e4f795 100644 --- a/sys/boot/powerpc/ofw/Makefile +++ b/sys/boot/powerpc/ofw/Makefile @@ -10,6 +10,7 @@ INSTALLFLAGS= -b # Architecture-specific loader code SRCS= conf.c metadata.c vers.c start.c +SRCS+= ucmpdi2.c LOADER_DISK_SUPPORT?= yes LOADER_UFS_SUPPORT?= yes @@ -62,9 +63,9 @@ LIBFICL= ${.OBJDIR}/../../ficl/libficl.a CFLAGS+= -DNETIF_OPEN_CLOSE_ONCE # Always add MI sources -.PATH: ${.CURDIR}/../../common +.PATH: ${.CURDIR}/../../common ${.CURDIR}/../../../libkern .include "${.CURDIR}/../../common/Makefile.inc" -CFLAGS+= -I${.CURDIR}/../../common +CFLAGS+= -I${.CURDIR}/../../common -I${.CURDIR}/../../.. CFLAGS+= -I. CLEANFILES+= vers.c loader.help diff --git a/sys/boot/powerpc/ofw/conf.c b/sys/boot/powerpc/ofw/conf.c index 4ab3e2334440..5666f4accae2 100644 --- a/sys/boot/powerpc/ofw/conf.c +++ b/sys/boot/powerpc/ofw/conf.c @@ -96,6 +96,7 @@ struct netif_driver *netif_drivers[] = { struct file_format *file_formats[] = { &ofw_elf, + &ofw_elf64, NULL }; diff --git a/sys/boot/powerpc/ofw/ldscript.powerpc b/sys/boot/powerpc/ofw/ldscript.powerpc index ecf8129455aa..e3b7b2c4b4c2 100644 --- a/sys/boot/powerpc/ofw/ldscript.powerpc +++ b/sys/boot/powerpc/ofw/ldscript.powerpc @@ -1,7 +1,7 @@ /* $FreeBSD$ */ OUTPUT_FORMAT("elf32-powerpc", "elf32-powerpc", "elf32-powerpc") -OUTPUT_ARCH(powerpc) +OUTPUT_ARCH(powerpc:common) ENTRY(_start) SEARCH_DIR(/usr/lib); /* Do we need any of these for elf? diff --git a/sys/boot/powerpc/ofw/metadata.c b/sys/boot/powerpc/ofw/metadata.c index 37c2bb717000..caed9cd0a41e 100644 --- a/sys/boot/powerpc/ofw/metadata.c +++ b/sys/boot/powerpc/ofw/metadata.c @@ -175,6 +175,9 @@ md_copyenv(vm_offset_t addr) * MOD_SIZE sizeof(size_t) module size * MOD_METADATA (variable) type-specific metadata */ + +static int align; + #define COPY32(v, a, c) { \ u_int32_t x = (v); \ if (c) \ @@ -187,7 +190,7 @@ md_copyenv(vm_offset_t addr) COPY32(strlen(s) + 1, a, c) \ if (c) \ archsw.arch_copyin(s, a, strlen(s) + 1);\ - a += roundup(strlen(s) + 1, sizeof(u_long));\ + a += roundup(strlen(s) + 1, align); \ } #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c) @@ -199,7 +202,7 @@ md_copyenv(vm_offset_t addr) COPY32(sizeof(s), a, c); \ if (c) \ archsw.arch_copyin(&s, a, sizeof(s)); \ - a += roundup(sizeof(s), sizeof(u_long)); \ + a += roundup(sizeof(s), align); \ } #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c) @@ -210,7 +213,7 @@ md_copyenv(vm_offset_t addr) COPY32(mm->md_size, a, c); \ if (c) \ archsw.arch_copyin(mm->md_data, a, mm->md_size);\ - a += roundup(mm->md_size, sizeof(u_long)); \ + a += roundup(mm->md_size, align); \ } #define MOD_END(a, c) { \ @@ -219,10 +222,11 @@ md_copyenv(vm_offset_t addr) } vm_offset_t -md_copymodules(vm_offset_t addr) +md_copymodules(vm_offset_t addr, int kern64) { struct preloaded_file *fp; struct file_metadata *md; + uint64_t scratch64; int c; c = addr != 0; @@ -233,8 +237,15 @@ md_copymodules(vm_offset_t addr) MOD_TYPE(addr, fp->f_type, c); if (fp->f_args) MOD_ARGS(addr, fp->f_args, c); - MOD_ADDR(addr, fp->f_addr, c); - MOD_SIZE(addr, fp->f_size, c); + if (kern64) { + scratch64 = fp->f_addr; + MOD_ADDR(addr, scratch64, c); + scratch64 = fp->f_size; + MOD_SIZE(addr, scratch64, c); + } else { + MOD_ADDR(addr, fp->f_addr, c); + MOD_SIZE(addr, fp->f_size, c); + } for (md = fp->f_metadata; md != NULL; md = md->md_next) { if (!(md->md_type & MODINFOMD_NOCOPY)) { MOD_METADATA(addr, md, c); @@ -254,7 +265,7 @@ md_copymodules(vm_offset_t addr) * - Module metadata are formatted and placed in kernel space. */ int -md_load(char *args, vm_offset_t *modulep) +md_load_dual(char *args, vm_offset_t *modulep, int kern64) { struct preloaded_file *kfp; struct preloaded_file *xp; @@ -263,11 +274,11 @@ md_load(char *args, vm_offset_t *modulep) vm_offset_t addr; vm_offset_t envp; vm_offset_t size; + uint64_t scratch64; char *rootdevname; int howto; - int dtlb_slots; - int itlb_slots; + align = kern64 ? 8 : 4; howto = md_getboothowto(args); /* @@ -298,23 +309,48 @@ md_load(char *args, vm_offset_t *modulep) addr = roundup(addr, PAGE_SIZE); kernend = 0; - kfp = file_findfile(NULL, "elf32 kernel"); + kfp = file_findfile(NULL, kern64 ? "elf64 kernel" : "elf32 kernel"); if (kfp == NULL) kfp = file_findfile(NULL, "elf kernel"); if (kfp == NULL) panic("can't find kernel file"); file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto); - file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp); - file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend); + if (kern64) { + scratch64 = envp; + file_addmetadata(kfp, MODINFOMD_ENVP, sizeof scratch64, &scratch64); + scratch64 = kernend; + file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof scratch64, &scratch64); + } else { + file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp); + file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend); + } *modulep = addr; - size = md_copymodules(0); + size = md_copymodules(0, kern64); kernend = roundup(addr + size, PAGE_SIZE); md = file_findmetadata(kfp, MODINFOMD_KERNEND); - bcopy(&kernend, md->md_data, sizeof kernend); - - (void)md_copymodules(addr); + if (kern64) { + scratch64 = kernend; + bcopy(&scratch64, md->md_data, sizeof scratch64); + } else { + bcopy(&kernend, md->md_data, sizeof kernend); + } + + (void)md_copymodules(addr, kern64); return(0); } + +int +md_load(char *args, vm_offset_t *modulep) +{ + return (md_load_dual(args, modulep, 0)); +} + +int +md_load64(char *args, vm_offset_t *modulep) +{ + return (md_load_dual(args, modulep, 1)); +} + diff --git a/sys/boot/powerpc/uboot/Makefile b/sys/boot/powerpc/uboot/Makefile index afd1b97aa89d..6ba738fd39a7 100644 --- a/sys/boot/powerpc/uboot/Makefile +++ b/sys/boot/powerpc/uboot/Makefile @@ -10,6 +10,7 @@ NO_MAN= # Architecture-specific loader code SRCS= start.S conf.c vers.c +SRCS+= ucmpdi2.c LOADER_DISK_SUPPORT?= yes LOADER_UFS_SUPPORT?= yes @@ -68,9 +69,9 @@ LIBFICL= ${.OBJDIR}/../../ficl/libficl.a .endif # Always add MI sources -.PATH: ${.CURDIR}/../../common +.PATH: ${.CURDIR}/../../common ${.CURDIR}/../../../libkern .include "${.CURDIR}/../../common/Makefile.inc" -CFLAGS+= -I${.CURDIR}/../../common +CFLAGS+= -I${.CURDIR}/../../common -I${.CURDIR}/../../.. CFLAGS+= -I. CLEANFILES+= vers.c ${PROG}.help diff --git a/sys/boot/powerpc/uboot/ldscript.powerpc b/sys/boot/powerpc/uboot/ldscript.powerpc index 43ef422a7f7f..fdba57c0076e 100644 --- a/sys/boot/powerpc/uboot/ldscript.powerpc +++ b/sys/boot/powerpc/uboot/ldscript.powerpc @@ -1,7 +1,7 @@ /* $FreeBSD$ */ OUTPUT_FORMAT("elf32-powerpc", "elf32-powerpc", "elf32-powerpc") -OUTPUT_ARCH(powerpc) +OUTPUT_ARCH(powerpc:common) ENTRY(_start) SEARCH_DIR(/usr/lib); PROVIDE (__stack = 0); diff --git a/sys/boot/uboot/Makefile.inc b/sys/boot/uboot/Makefile.inc index 265f86d1ed55..dab65e52621d 100644 --- a/sys/boot/uboot/Makefile.inc +++ b/sys/boot/uboot/Makefile.inc @@ -1,3 +1,8 @@ # $FreeBSD$ +.if ${MACHINE_ARCH} == "powerpc64" +CFLAGS+= -m32 -mcpu=powerpc +LDFLAGS+= -m elf32ppc +.endif + .include "../Makefile.inc"