asst3-Virtual Memory Translation/kern/syscall/runprogram.c
int
runprogram(char *progname)
{
	struct argbuf kargv;
	vaddr_t entrypoint, stackptr;
	int argc;
	userptr_t uargv;
	int result;

	/* We must be a thread that can run in a user process. */
	KASSERT(curproc->p_pid >= PID_MIN && curproc->p_pid <= PID_MAX);

	/* We should be a new process. */
	KASSERT(proc_getas() == NULL);

	/* Set up stdin/stdout/stderr if necessary. */
	if (curproc->p_filetable == NULL) {
		curproc->p_filetable = filetable_create();
		if (curproc->p_filetable == NULL) {
			return ENOMEM;
		}

		result = open_stdfds("con:", "con:", "con:");
		if (result) {
			return result;
		}
	}

	/*
	 * Cons up argv.
	 */

	argbuf_init(&kargv);
	result = argbuf_fromkernel(&kargv, progname);
	if (result) {
		argbuf_cleanup(&kargv);
		return result;
	}

	/* Load the executable. Note: must not fail after this succeeds. */
	result = loadexec(progname, &entrypoint, &stackptr);
	if (result) {
		argbuf_cleanup(&kargv);
		return result;
	}

	result = argbuf_copyout(&kargv, &stackptr, &argc, &uargv);
	if (result) {
		/* If copyout fails, *we* messed up, so panic */
		panic("execv: copyout_args failed: %s\n", strerror(result));
	}

	/* free the space */
	argbuf_cleanup(&kargv);

	/* Warp to user mode. */
	enter_new_process(argc, uargv, NULL /*uenv*/, stackptr, entrypoint);

	/* enter_new_process does not return. */
	panic("enter_new_process returned\n");
	return EINVAL;
}
/*
 * Load program "progname" and start running it in usermode.
 * Does not return except on error.
 *
 * Opens the standard file descriptors if necessary.
 *
 * Calls vfs_open on PROGNAME (via loadexec) and thus may destroy it,
 * so it needs to be mutable.
 */
Load program "progname" and start running it in usermode.
Does not return except on error.

Opens the standard file descriptors if necessary.

Calls vfs_open on PROGNAME (via loadexec) and thus may destroy it,
so it needs to be mutable.
Load program " progname " and start running it in usermode . Does not return except on error . Opens the standard file descriptors if necessary . Calls vfs_open on PROGNAME ( via loadexec ) and thus may destroy it so it needs to be mutable .
int
runprogram(char *progname)
{
	struct argbuf kargv;
	vaddr_t entrypoint, stackptr;
	int argc;
	userptr_t uargv;
	int result;
	KASSERT(curproc->p_pid >= PID_MIN && curproc->p_pid <= PID_MAX);
	KASSERT(proc_getas() == NULL);
	if (curproc->p_filetable == NULL) {
		curproc->p_filetable = filetable_create();
		if (curproc->p_filetable == NULL) {
			return ENOMEM;
		}
		result = open_stdfds("con:", "con:", "con:");
		if (result) {
			return result;
		}
	}
	argbuf_init(&kargv);
	result = argbuf_fromkernel(&kargv, progname);
	if (result) {
		argbuf_cleanup(&kargv);
		return result;
	}
	result = loadexec(progname, &entrypoint, &stackptr);
	if (result) {
		argbuf_cleanup(&kargv);
		return result;
	}
	result = argbuf_copyout(&kargv, &stackptr, &argc, &uargv);
	if (result) {
		panic("execv: copyout_args failed: %s\n", strerror(result));
	}
	argbuf_cleanup(&kargv);
	enter_new_process(argc, uargv, NULL , stackptr, entrypoint);
	panic("enter_new_process returned\n");
	return EINVAL;
}
int runprogram ( char * progname ) { struct argbuf kargv ; vaddr_t entrypoint , stackptr ; int argc ; userptr_t uargv ; int result ; KASSERT ( curproc -> p_pid >= PID_MIN && curproc -> p_pid <= PID_MAX ) ; KASSERT ( proc_getas ( ) == NULL ) ; if ( curproc -> p_filetable == NULL ) { curproc -> p_filetable = filetable_create ( ) ; if ( curproc -> p_filetable == NULL ) { return ENOMEM ; } result = open_stdfds ( " " , " " , " " ) ; if ( result ) { return result ; } } argbuf_init ( & kargv ) ; result = argbuf_fromkernel ( & kargv , progname ) ; if ( result ) { argbuf_cleanup ( & kargv ) ; return result ; } result = loadexec ( progname , & entrypoint , & stackptr ) ; if ( result ) { argbuf_cleanup ( & kargv ) ; return result ; } result = argbuf_copyout ( & kargv , & stackptr , & argc , & uargv ) ; if ( result ) { panic ( " \n " , strerror ( result ) ) ; } argbuf_cleanup ( & kargv ) ; enter_new_process ( argc , uargv , NULL , stackptr , entrypoint ) ; panic ( " \n " ) ; return EINVAL ; }
Load program "progname" and start running it in usermode.
Load program " progname " and start running it in usermode .
/* We must be a thread that can run in a user process. */ /* We should be a new process. */ /* Set up stdin/stdout/stderr if necessary. */ /*
	 * Cons up argv.
	 */ /* Load the executable. Note: must not fail after this succeeds. */ /* If copyout fails, *we* messed up, so panic */ /* free the space */ /* Warp to user mode. */ /*uenv*/ /* enter_new_process does not return. */