Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/arch/amd64/stand Add the amd64 prekern. It is a kernel r...
details: https://anonhg.NetBSD.org/src/rev/32b9ec2d23b8
branches: trunk
changeset: 356739:32b9ec2d23b8
user: maxv <maxv%NetBSD.org@localhost>
date: Tue Oct 10 09:29:14 2017 +0000
description:
Add the amd64 prekern. It is a kernel relocator used for Kernel ASLR (see
tech-kern@). It works, but is not yet linked to the build system, because
I can't build a distribution right now.
diffstat:
sys/arch/amd64/stand/Makefile | 5 +
sys/arch/amd64/stand/prekern/Makefile | 40 +
sys/arch/amd64/stand/prekern/console.c | 120 +++++
sys/arch/amd64/stand/prekern/elf.c | 520 ++++++++++++++++++++++
sys/arch/amd64/stand/prekern/locore.S | 602 ++++++++++++++++++++++++++
sys/arch/amd64/stand/prekern/mm.c | 227 +++++++++
sys/arch/amd64/stand/prekern/pdir.h | 97 ++++
sys/arch/amd64/stand/prekern/prekern.c | 382 ++++++++++++++++
sys/arch/amd64/stand/prekern/prekern.h | 112 ++++
sys/arch/amd64/stand/prekern/prekern.ldscript | 54 ++
sys/arch/amd64/stand/prekern/redef.h | 47 ++
sys/arch/amd64/stand/prekern/trap.S | 194 ++++++++
12 files changed, 2400 insertions(+), 0 deletions(-)
diffs (truncated from 2448 to 300 lines):
diff -r 47589a6f5204 -r 32b9ec2d23b8 sys/arch/amd64/stand/Makefile
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/amd64/stand/Makefile Tue Oct 10 09:29:14 2017 +0000
@@ -0,0 +1,5 @@
+# $NetBSD: Makefile,v 1.1 2017/10/10 09:29:14 maxv Exp $
+
+SUBDIR= prekern
+
+.include <bsd.subdir.mk>
diff -r 47589a6f5204 -r 32b9ec2d23b8 sys/arch/amd64/stand/prekern/Makefile
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/amd64/stand/prekern/Makefile Tue Oct 10 09:29:14 2017 +0000
@@ -0,0 +1,40 @@
+# $NetBSD: Makefile,v 1.1 2017/10/10 09:29:14 maxv Exp $
+
+PROG= prekern
+SRCS= locore.S trap.S prekern.c mm.c console.c elf.c
+
+NOSSP= # defined
+NOPIE= # defined
+NOMAN= 1
+
+S= ${.CURDIR}/../../../..
+
+.PATH: ${.CURDIR}
+
+BINDIR= /usr/mdec
+BINMODE= 444
+
+.include <bsd.own.mk>
+
+CPPFLAGS+= -I. -I${S}
+
+.include <bsd.klinks.mk>
+
+CPPFLAGS+= -DKERNEL -D__x86_64__
+CFLAGS+= -Wall -Werror -mno-red-zone -mno-mmx -mno-sse -mno-avx -ffreestanding
+STRIPFLAG=
+LINKFLAGS= -X -z max-page-size=0x100000 -Ttext 0x100000 -T prekern.ldscript
+
+LIBCRT0= # nothing
+LIBCRTI= # nothing
+LIBC= # nothing
+LIBCRTBEGIN= # nothing
+LIBCRTEND= # nothing
+
+${PROG}: ${OBJS}
+ ${LD} ${LINKFLAGS} -o ${.TARGET} ${OBJS}
+
+all: ${PROG}
+
+.include <bsd.prog.mk>
+
diff -r 47589a6f5204 -r 32b9ec2d23b8 sys/arch/amd64/stand/prekern/console.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/amd64/stand/prekern/console.c Tue Oct 10 09:29:14 2017 +0000
@@ -0,0 +1,120 @@
+/* $NetBSD: console.c,v 1.1 2017/10/10 09:29:14 maxv Exp $ */
+
+/*
+ * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Maxime Villard.
+ *
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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 "prekern.h"
+
+extern vaddr_t atdevbase;
+#define CONS_WID 80
+#define CONS_HEI 25
+
+static char *cons_start;
+static size_t cons_x, cons_y;
+static char cons_buffer[CONS_WID * 2 * CONS_HEI];
+
+void init_cons()
+{
+ cons_start = (char *)atdevbase + (0xB8000 - IOM_BEGIN);
+ cons_x = 0;
+ cons_y = 0;
+}
+
+static void check_scroll()
+{
+ char *src, *dst;
+ size_t i;
+
+ if (cons_y != CONS_HEI)
+ return;
+
+ for (i = 0; i < CONS_HEI-1; i++) {
+ dst = &cons_buffer[0] + i * (CONS_WID * 2);
+ src = &cons_buffer[0] + (i + 1) * (CONS_WID * 2);
+ memcpy(dst, src, (CONS_WID * 2));
+ }
+ memset(&cons_buffer[0] + (CONS_WID * 2) * (CONS_HEI-1), 0,
+ (CONS_WID * 2));
+ cons_y--;
+ memcpy(cons_start, &cons_buffer[0], (CONS_WID * 2) * (CONS_HEI-1));
+}
+
+void print_ext(int color, char *buf)
+{
+ char *ptr, *scr;
+ size_t i;
+
+ for (i = 0; buf[i] != '\0'; i++) {
+ if (buf[i] == '\n') {
+ cons_x = 0;
+ cons_y++;
+ check_scroll();
+ } else {
+ if (cons_x + 1 == CONS_WID) {
+ cons_x = 0;
+ cons_y++;
+ check_scroll();
+ }
+ ptr = (cons_start + 2 * cons_x + 160 * cons_y);
+ scr = (cons_buffer + 2 * cons_x + 160 * cons_y);
+ ptr[0] = scr[0] = buf[i];
+ ptr[1] = scr[1] = color;
+ cons_x++;
+ }
+ }
+}
+
+void print(char *buf)
+{
+ print_ext(WHITE_ON_BLACK, buf);
+}
+
+void print_state(bool ok, char *buf)
+{
+ print("[");
+ if (ok)
+ print_ext(GREEN_ON_BLACK, "+");
+ else
+ print_ext(RED_ON_BLACK, "!");
+ print("] ");
+ print(buf);
+ print("\n");
+}
+
+void print_banner()
+{
+ char *banner =
+ " __________ __ \n"
+ " \\______ \\_______ ____ | | __ ___________ ____ \n"
+ " | ___/\\_ __ \\_/ __ \\| |/ // __ \\_ __ \\/ \\ \n"
+ " | | | | \\/\\ ___/| <\\ ___/| | \\/ | \\\n"
+ " |____| |__| \\___ >__|_ \\\\___ >__| |___| /\n"
+ " \\/ \\/ \\/ \\/ Version 1.0\n"
+ ;
+ print(banner);
+}
diff -r 47589a6f5204 -r 32b9ec2d23b8 sys/arch/amd64/stand/prekern/elf.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/amd64/stand/prekern/elf.c Tue Oct 10 09:29:14 2017 +0000
@@ -0,0 +1,520 @@
+/* $NetBSD: elf.c,v 1.1 2017/10/10 09:29:14 maxv Exp $ */
+
+/*
+ * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Maxime Villard.
+ *
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+
+#define ELFSIZE 64
+
+#include "prekern.h"
+#include <sys/exec_elf.h>
+
+struct elfinfo {
+ Elf_Ehdr *ehdr;
+ Elf_Shdr *shdr;
+ char *shstrtab;
+ size_t shstrsz;
+ Elf_Sym *symtab;
+ size_t symcnt;
+ char *strtab;
+ size_t strsz;
+ struct {
+ vaddr_t va;
+ size_t sz;
+ } text;
+ struct {
+ vaddr_t va;
+ size_t sz;
+ } rodata;
+ struct {
+ vaddr_t va;
+ size_t sz;
+ } data;
+};
+
+static struct elfinfo eif;
+static const char entrypoint[] = "start_prekern";
+
+/* XXX */
+static int
+memcmp(const char *a, const char *b, size_t c)
+{
+ size_t i;
+ for (i = 0; i < c; i++) {
+ if (a[i] != b[i])
+ return 1;
+ }
+ return 0;
+}
+static int
+strcmp(char *a, char *b)
+{
+ size_t i;
+ for (i = 0; a[i] != '\0'; i++) {
+ if (a[i] != b[i])
+ return 1;
+ }
+ return 0;
+}
+
+
+static int
+elf_check_header()
+{
+ if (memcmp((char *)eif.ehdr->e_ident, ELFMAG, SELFMAG) != 0 ||
+ eif.ehdr->e_ident[EI_CLASS] != ELFCLASS) {
+ return -1;
+ }
+ return 0;
+}
+
+static vaddr_t
+elf_get_entrypoint()
+{
+ Elf_Sym *sym;
+ size_t i;
+ char *buf;
+
+ for (i = 0; i < eif.symcnt; i++) {
+ sym = &eif.symtab[i];
+
+ if (ELF_ST_TYPE(sym->st_info) != STT_FUNC)
+ continue;
+ if (sym->st_name == 0)
+ continue;
+ if (sym->st_shndx == SHN_UNDEF)
+ continue; /* Skip external references */
+ buf = eif.strtab + sym->st_name;
+
+ if (!memcmp(buf, entrypoint, sizeof(entrypoint))) {
+ return (vaddr_t)sym->st_value;
+ }
+ }
+
+ return 0;
+}
Home |
Main Index |
Thread Index |
Old Index