From 9c059ae3427edcf411a274e628ade7953edf3635 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Mon, 4 Dec 2006 18:04:05 +0000 Subject: [PATCH] Misc vkernel work. --- sys/cpu/i386/include/param.h | 10 +- sys/platform/vkernel/i386/locore.s | 17 +- sys/platform/vkernel/include/vmparam.h | 5 +- sys/platform/vkernel/platform/init.c | 218 +++++++++++++++++++++++++ sys/sys/vkernel.h | 7 +- 5 files changed, 239 insertions(+), 18 deletions(-) create mode 100644 sys/platform/vkernel/platform/init.c diff --git a/sys/cpu/i386/include/param.h b/sys/cpu/i386/include/param.h index 4324d9b920..4811dc5793 100644 --- a/sys/cpu/i386/include/param.h +++ b/sys/cpu/i386/include/param.h @@ -35,7 +35,7 @@ * * from: @(#)param.h 5.8 (Berkeley) 6/28/91 * $FreeBSD: src/sys/i386/include/param.h,v 1.54.2.8 2002/08/31 21:15:55 dillon Exp $ - * $DragonFly: src/sys/cpu/i386/include/param.h,v 1.10 2006/11/07 06:43:22 dillon Exp $ + * $DragonFly: src/sys/cpu/i386/include/param.h,v 1.11 2006/12/04 18:04:00 dillon Exp $ */ #ifndef _CPU_PARAM_H_ @@ -101,11 +101,15 @@ #define ALIGNBYTES _ALIGNBYTES #define ALIGN(p) _ALIGN(p) -#define PAGE_SHIFT 12 /* LOG2(PAGE_SIZE) */ -#define PAGE_SIZE (1< @@ -43,16 +43,13 @@ .data ALIGN_DATA /* just to be sure */ + /* + * Normally the startup code would begin here, but this is a + * virtual kernel so we just have a main() in platform/init.c + */ + .text -NON_GPROF_ENTRY(btext) - - call initvkernel - call mi_startup -1: - hlt - jmp 1b -#if 0 /* * Signal trampoline, copied to top of user stack */ @@ -77,5 +74,3 @@ esigcode: szsigcode: .long esigcode - sigcode -#endif - diff --git a/sys/platform/vkernel/include/vmparam.h b/sys/platform/vkernel/include/vmparam.h index 0835143ea5..c71390dd2b 100644 --- a/sys/platform/vkernel/include/vmparam.h +++ b/sys/platform/vkernel/include/vmparam.h @@ -31,7 +31,7 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/sys/platform/vkernel/include/vmparam.h,v 1.1 2006/11/07 18:50:07 dillon Exp $ + * $DragonFly: src/sys/platform/vkernel/include/vmparam.h,v 1.2 2006/12/04 18:04:03 dillon Exp $ */ #ifndef _MACHINE_VMPARAM_H_ @@ -74,8 +74,9 @@ * address spaces exist in different VM spaces and can overlap. */ #define KERNBASE 0x1000 +#define KERNEL_KVA_SIZE 0x40000000 -#define VM_MIN_KERNEL_ADDRESS 0 +#define VM_MIN_KERNEL_ADDRESS 0x00000000 #define VM_MAX_KERNEL_ADDRESS 0xC0000000 #define VM_MIN_USER_ADDRESS 0x00000000 diff --git a/sys/platform/vkernel/platform/init.c b/sys/platform/vkernel/platform/init.c new file mode 100644 index 0000000000..c249304905 --- /dev/null +++ b/sys/platform/vkernel/platform/init.c @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2006 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * 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. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDERS 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. + * + * $DragonFly: src/sys/platform/vkernel/platform/init.c,v 1.1 2006/12/04 18:04:04 dillon Exp $ + */ + +#include + +vm_paddr_t Maxmem; +int MemImageFd = -1; +int RootImageFd = -1; +vm_offset_t KvaBase; +vm_offset_t KvaSize; + +static void init_sys_memory(const char *imageFile); +static void init_kern_memory(const char *imageFile); +static void init_rootdevice(const char *imageFile); + +/* + * Kernel startup for virtual kernels - standard main() + */ +int +main(int ac, char **av) +{ + const char *memImageFile; + const char *rootImageFile; + + /* + * Process options + */ + while ((c = getopt(ac, av, "vm:")) != -1) { + switch(c) { + case 'v': + bootverbose = 1; + break; + case 'i': + memImageFile = optarg; + break; + case 'r': + rootImageFile = optarg; + break; + case 'm': + Maxmem = strtoull(optarg, &suffix, 0); + if (suffix) { + switch(*suffix) { + case 'g': + case 'G': + Maxmem <<= 30; + break; + case 'm': + case 'M': + Maxmem <<= 20; + break; + case 'k': + case 'K': + Maxmem <<= 10; + break; + default: + Maxmem = 0; + usage("Bad maxmem option"); + /* NOT REACHED */ + break; + } + } + break; + } + } + + init_sys_memory(memImageFile); + init_kern_memory(); + init_rootdevice(rootImageFile); + mi_startup(); + /* NOT REACHED */ +} + +/* + * Initialize system memory. This is the virtual kernel's 'RAM'. + */ +static +void +init_sys_memory(const char *imageFile) +{ + int fd; + + /* + * Figure out the system memory image size. If an image file was + * specified and -m was not specified, use the image file's size. + */ + + if (imageFile && stat(imageFile, &st) == 0 && Maxmem == 0) + Maxmem = (vm_paddr_t)st.st_size; + if ((imageFile == NULL || stat(imageFile, &st) < 0) && Maxmem == 0) { + err(1, "Cannot create new memory file unless " + "system memory size is specified with -m", + imageFile); + /* NOT REACHED */ + } + + /* + * Maxmem must be known at this time + */ + if (Maxmem < 32 * 1024 * 1024 || (Maxmem & SEG_MASK)) { + err(1, "Bad maxmem specification: 32MB minimum, " + "multiples of %dMB only", + SEG_SIZE / 1024 / 1024); + /* NOT REACHED */ + } + + /* + * Generate an image file name if necessary, then open/create the + * file exclusively locked. Do not allow multiple virtual kernels + * to use the same image file. + */ + if (imageFile == NULL) + asprintf(&imageFile, "/var/vkernel/image.%05d", (int)getpid()); + fd = open(imageFile, O_RDWR|O_CREAT|O_EXLOCK|O_NONBLOCK, 0644); + if (fd < 0 || fstat(fd, &st) < 0) { + err(1, "Unable to open/create %s", + imageFile, strerror(errno)); + /* NOT REACHED */ + } + + /* + * Truncate or extend the file as necessary. + */ + if (st.st_size > Maxmem) { + ftruncate(fd, Maxmem); + } else if (st.st_size < Maxmem) { + char *zmem; + off_t off = st.st_size & ~SEG_MASK; + + printf("%s: Reserving blocks for memory image\n", imageFile); + zmem = malloc(SEG_SIZE); + bzero(zmem, SEG_SIZE); + lseek(fd, off, 0); + while (off < Maxmem) { + if (write(fd, zmem, SEG_SIZE) != SEG_SIZE) { + err(1, "Unable to reserve blocks for memory image"); + /* NOT REACHED */ + } + off += SEG_SIZE; + } + if (fsync(fd) < 0) + err(1, "Unable to reserve blocks for memory image"); + free(zmem); + } + MemImageFd = fd; +} + +/* + * Initialize kernel memory. This reserves kernel virtual memory by using + * MAP_VPAGETABLE + */ +static +void +init_kern_memory(void) +{ + void *base; + + /* + * Memory map our kernel virtual memory space. Note that the + * kernel image itself is not made part of this memory for the + * moment. + */ + base = mmap(NULL, KERNEL_KVA_SIZE, PROT_READ|PROT_WRITE, + MAP_FILE|MAP_VPAGETABLE, MemImageFd, 0); + if (base == MAP_FAILED) { + err(1, "Unable to mmap() kernel virtual memory!"); + /* NOT REACHED */ + } + KvaBase = (vm_offset_t)base; + KvaSize = KERNEL_KVA_SIZE; + + /* + * Create a top-level page table self-mapping itself. + */ +} + +/* + * The root filesystem path for the virtual kernel is optional. If specified + * it points to a filesystem image. + */ +static +void +init_rootdevice(const char *imageFile) +{ +} + diff --git a/sys/sys/vkernel.h b/sys/sys/vkernel.h index 97a3193486..490cf84e7d 100644 --- a/sys/sys/vkernel.h +++ b/sys/sys/vkernel.h @@ -31,7 +31,7 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/sys/sys/vkernel.h,v 1.4 2006/10/20 17:02:13 dillon Exp $ + * $DragonFly: src/sys/sys/vkernel.h,v 1.5 2006/12/04 18:04:05 dillon Exp $ */ #ifndef _SYS_VKERNEL_H_ @@ -118,8 +118,11 @@ typedef u_int32_t vpte_t; #define VPTE_PAGE_BITS 10 #define VPTE_PAGE_MASK ((1 << VPTE_PAGE_BITS) - 1) -#define VPTE_V 0x00000001 /* inverted valid bit (TEMPORARY) */ +#define VPTE_V 0x00000001 #define VPTE_PS 0x00000002 +#define VPTE_R 0x00000004 +#define VPTE_W 0x00000008 +#define VPTE_X 0x00000010 #endif -- 2.41.0