2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
13 #include <sys/ptrace.h>
14 #include <sys/utsname.h>
15 #include <sys/param.h>
17 #include "asm/types.h"
26 #include "user_util.h"
27 #include "kern_util.h"
32 #include "uml-config.h"
34 #define COMMAND_LINE_SIZE _POSIX_ARG_MAX
36 /* Changed in linux_main and setup_arch, which run before SMP is started */
37 char saved_command_line[COMMAND_LINE_SIZE] = { 0 };
38 char command_line[COMMAND_LINE_SIZE] = { 0 };
40 void add_arg(char *cmd_line, char *arg)
42 if (strlen(cmd_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
43 printf("add_arg: Too much command line!\n");
46 if(strlen(cmd_line) > 0) strcat(cmd_line, " ");
47 strcat(cmd_line, arg);
52 while(1) sleep(1000000);
55 void stack_protections(unsigned long address)
57 int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
59 if(mprotect((void *) address, page_size(), prot) < 0)
60 panic("protecting stack failed, errno = %d", errno);
63 void task_protections(unsigned long address)
65 unsigned long guard = address + page_size();
66 unsigned long stack = guard + page_size();
70 if(mprotect((void *) stack, page_size(), prot) < 0)
71 panic("protecting guard page failed, errno = %d", errno);
73 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER) - 2;
74 prot = PROT_READ | PROT_WRITE | PROT_EXEC;
75 if(mprotect((void *) stack, pages * page_size(), prot) < 0)
76 panic("protecting stack failed, errno = %d", errno);
79 int wait_for_stop(int pid, int sig, int cont_type, void *relay)
81 sigset_t *relay_signals = relay;
85 if(((ret = waitpid(pid, &status, WUNTRACED)) < 0) ||
86 !WIFSTOPPED(status) || (WSTOPSIG(status) != sig)){
88 if(errno == EINTR) continue;
89 printk("wait failed, errno = %d\n",
92 else if(WIFEXITED(status))
93 printk("process exited with status %d\n",
95 else if(WIFSIGNALED(status))
96 printk("process exited with signal %d\n",
98 else if((WSTOPSIG(status) == SIGVTALRM) ||
99 (WSTOPSIG(status) == SIGALRM) ||
100 (WSTOPSIG(status) == SIGIO) ||
101 (WSTOPSIG(status) == SIGPROF) ||
102 (WSTOPSIG(status) == SIGCHLD) ||
103 (WSTOPSIG(status) == SIGWINCH) ||
104 (WSTOPSIG(status) == SIGINT)){
105 ptrace(cont_type, pid, 0, WSTOPSIG(status));
108 else if((relay_signals != NULL) &&
109 sigismember(relay_signals, WSTOPSIG(status))){
110 ptrace(cont_type, pid, 0, WSTOPSIG(status));
113 else printk("process stopped with signal %d\n",
115 panic("wait_for_stop failed to wait for %d to stop "
116 "with %d\n", pid, sig);
122 int clone_and_wait(int (*fn)(void *), void *arg, void *sp, int flags)
126 pid = clone(fn, sp, flags, arg);
127 if(pid < 0) return(-1);
128 wait_for_stop(pid, SIGSTOP, PTRACE_CONT, NULL);
129 ptrace(PTRACE_CONT, pid, 0, 0);
133 int raw(int fd, int complain)
140 err = tcsetattr(fd, TCSANOW, &tt);
141 if((err < 0) && complain){
142 printk("tcsetattr failed, errno = %d\n", errno);
148 void setup_machinename(char *machine_out)
153 strcpy(machine_out, host.machine);
156 char host_info[(_UTSNAME_LENGTH + 1) * 4 + _UTSNAME_NODENAME_LENGTH + 1];
158 void setup_hostinfo(void)
163 sprintf(host_info, "%s %s %s %s %s", host.sysname, host.nodename,
164 host.release, host.version, host.machine);
168 * Overrides for Emacs so that we follow Linus's tabbing style.
169 * Emacs will notice this stuff at the end of the file and automatically
170 * adjust the settings for this buffer only. This must remain at the end
172 * ---------------------------------------------------------------------------
174 * c-file-style: "linux"