/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*30* Sample/test code for running a user program. You can use this for31* reference when implementing the execv() system call. Remember though32* that execv() needs to do more than this function does.33*/3435#include <types.h>36#include <kern/errno.h>37#include <kern/fcntl.h>38#include <lib.h>39#include <thread.h>40#include <current.h>41#include <addrspace.h>42#include <vm.h>43#include <vfs.h>44#include <syscall.h>45#include <test.h>4647/*48* Load program "progname" and start running it in usermode.49* Does not return except on error.50*51* Calls vfs_open on progname and thus may destroy it.52*/53int54runprogram(char *progname)55{56struct vnode *v;57vaddr_t entrypoint, stackptr;58int result;5960/* Open the file. */61result = vfs_open(progname, O_RDONLY, 0, &v);62if (result) {63return result;64}6566/* We should be a new thread. */67KASSERT(curthread->t_addrspace == NULL);6869/* Create a new address space. */70curthread->t_addrspace = as_create();71if (curthread->t_addrspace==NULL) {72vfs_close(v);73return ENOMEM;74}7576/* Activate it. */77as_activate(curthread->t_addrspace);7879/* Load the executable. */80result = load_elf(v, &entrypoint);81if (result) {82/* thread_exit destroys curthread->t_addrspace */83vfs_close(v);84return result;85}8687/* Done with the file now. */88vfs_close(v);8990/* Define the user stack in the address space */91result = as_define_stack(curthread->t_addrspace, &stackptr);92if (result) {93/* thread_exit destroys curthread->t_addrspace */94return result;95}9697/* Warp to user mode. */98enter_new_process(0 /*argc*/, NULL /*userspace addr of argv*/,99stackptr, entrypoint);100101/* enter_new_process does not return. */102panic("enter_new_process returned\n");103return EINVAL;104}105106107108