📄 process.c
字号:
/* * Copyright (C) 2002- 2004 Jeff Dike (jdike@addtoit.com) * Licensed under the GPL */#include <stdlib.h>#include <string.h>#include <unistd.h>#include <errno.h>#include <signal.h>#include <setjmp.h>#include <sched.h>#include <sys/wait.h>#include <sys/mman.h>#include <sys/user.h>#include <sys/time.h>#include <asm/unistd.h>#include <asm/types.h>#include "user.h"#include "ptrace_user.h"#include "time_user.h"#include "sysdep/ptrace.h"#include "user_util.h"#include "kern_util.h"#include "skas.h"#include "stub-data.h"#include "mm_id.h"#include "sysdep/sigcontext.h"#include "sysdep/stub.h"#include "os.h"#include "proc_mm.h"#include "skas_ptrace.h"#include "chan_user.h"#include "signal_user.h"#include "registers.h"#include "mem.h"#include "uml-config.h"#include "process.h"int is_skas_winch(int pid, int fd, void *data){ if(pid != os_getpgrp()) return(0); register_winch_irq(-1, fd, -1, data); return(1);}void wait_stub_done(int pid, int sig, char * fname){ int n, status, err; do { if ( sig != -1 ) { err = ptrace(PTRACE_CONT, pid, 0, sig); if(err) panic("%s : continue failed, errno = %d\n", fname, errno); } sig = 0; CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); } while((n >= 0) && WIFSTOPPED(status) && ((WSTOPSIG(status) == SIGVTALRM) || /* running UML inside a detached screen can cause * SIGWINCHes */ (WSTOPSIG(status) == SIGWINCH))); if((n < 0) || !WIFSTOPPED(status) || (WSTOPSIG(status) != SIGUSR1 && WSTOPSIG(status) != SIGTRAP)){ unsigned long regs[FRAME_SIZE]; if(ptrace(PTRACE_GETREGS, pid, 0, regs) < 0) printk("Failed to get registers from stub, " "errno = %d\n", errno); else { int i; printk("Stub registers -\n"); for(i = 0; i < FRAME_SIZE; i++) printk("\t%d - %lx\n", i, regs[i]); } panic("%s : failed to wait for SIGUSR1/SIGTRAP, " "pid = %d, n = %d, errno = %d, status = 0x%x\n", fname, pid, n, errno, status); }}void get_skas_faultinfo(int pid, struct faultinfo * fi){ int err; if(ptrace_faultinfo){ err = ptrace(PTRACE_FAULTINFO, pid, 0, fi); if(err) panic("get_skas_faultinfo - PTRACE_FAULTINFO failed, " "errno = %d\n", errno); /* Special handling for i386, which has different structs */ if (sizeof(struct ptrace_faultinfo) < sizeof(struct faultinfo)) memset((char *)fi + sizeof(struct ptrace_faultinfo), 0, sizeof(struct faultinfo) - sizeof(struct ptrace_faultinfo)); } else { wait_stub_done(pid, SIGSEGV, "get_skas_faultinfo"); /* faultinfo is prepared by the stub-segv-handler at start of * the stub stack page. We just have to copy it. */ memcpy(fi, (void *)current_stub_stack(), sizeof(*fi)); }}static void handle_segv(int pid, union uml_pt_regs * regs){ get_skas_faultinfo(pid, ®s->skas.faultinfo); segv(regs->skas.faultinfo, 0, 1, NULL);}/*To use the same value of using_sysemu as the caller, ask it that value (in local_using_sysemu)*/static void handle_trap(int pid, union uml_pt_regs *regs, int local_using_sysemu){ int err, status; /* Mark this as a syscall */ UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->skas.regs); if (!local_using_sysemu) { err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET, __NR_getpid); if(err < 0) panic("handle_trap - nullifying syscall failed errno = %d\n", errno); err = ptrace(PTRACE_SYSCALL, pid, 0, 0); if(err < 0) panic("handle_trap - continuing to end of syscall failed, " "errno = %d\n", errno); CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED)); if((err < 0) || !WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP + 0x80)) panic("handle_trap - failed to wait at end of syscall, " "errno = %d, status = %d\n", errno, status); } handle_syscall(regs);}extern int __syscall_stub_start;int stub_code_fd = -1;__u64 stub_code_offset;static int userspace_tramp(void *stack){ void *addr; ptrace(PTRACE_TRACEME, 0, 0, 0); init_new_thread_signals(1); enable_timer(); if(!proc_mm){ /* This has a pte, but it can't be mapped in with the usual * tlb_flush mechanism because this is part of that mechanism */ addr = mmap64((void *) UML_CONFIG_STUB_CODE, page_size(), PROT_EXEC, MAP_FIXED | MAP_PRIVATE, stub_code_fd, stub_code_offset); if(addr == MAP_FAILED){ printk("mapping stub code failed, errno = %d\n", errno); exit(1); } if(stack != NULL){ int fd; __u64 offset; fd = phys_mapping(to_phys(stack), &offset); addr = mmap((void *) UML_CONFIG_STUB_DATA, page_size(), PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, fd, offset); if(addr == MAP_FAILED){ printk("mapping stub stack failed, " "errno = %d\n", errno); exit(1); } } } if(!ptrace_faultinfo){ unsigned long v = UML_CONFIG_STUB_CODE + (unsigned long) stub_segv_handler - (unsigned long) &__syscall_stub_start; set_sigstack((void *) UML_CONFIG_STUB_DATA, page_size()); set_handler(SIGSEGV, (void *) v, SA_ONSTACK, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, SIGUSR1, -1); } os_stop_process(os_getpid()); return(0);}/* Each element set once, and only accessed by a single processor anyway */#undef NR_CPUS#define NR_CPUS 1int userspace_pid[NR_CPUS];int start_userspace(unsigned long stub_stack){ void *stack; unsigned long sp; int pid, status, n, flags; if ( stub_code_fd == -1 ) stub_code_fd = phys_mapping(to_phys(&__syscall_stub_start), &stub_code_offset); stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if(stack == MAP_FAILED) panic("start_userspace : mmap failed, errno = %d", errno); sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *); flags = CLONE_FILES | SIGCHLD; if(proc_mm) flags |= CLONE_VM; pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack); if(pid < 0) panic("start_userspace : clone failed, errno = %d", errno); do { CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); if(n < 0) panic("start_userspace : wait failed, errno = %d", errno); } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM)); if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) panic("start_userspace : expected SIGSTOP, got status = %d", status); if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, (void *)PTRACE_O_TRACESYSGOOD) < 0) panic("start_userspace : PTRACE_SETOPTIONS failed, errno=%d\n", errno); if(munmap(stack, PAGE_SIZE) < 0) panic("start_userspace : munmap failed, errno = %d\n", errno); return(pid);}void userspace(union uml_pt_regs *regs){ int err, status, op, pid = userspace_pid[0]; int local_using_sysemu; /*To prevent races if using_sysemu changes under us.*/ while(1){ restore_registers(pid, regs); /* Now we set local_using_sysemu to be used for one loop */ local_using_sysemu = get_using_sysemu(); op = SELECT_PTRACE_OPERATION(local_using_sysemu, singlestepping(NULL)); err = ptrace(op, pid, 0, 0); if(err) panic("userspace - could not resume userspace process, " "pid=%d, ptrace operation = %d, errno = %d\n", op, errno); CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED)); if(err < 0) panic("userspace - waitpid failed, errno = %d\n", errno); regs->skas.is_user = 1; save_registers(pid, regs); UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */ if(WIFSTOPPED(status)){ switch(WSTOPSIG(status)){ case SIGSEGV: if(PTRACE_FULL_FAULTINFO || !ptrace_faultinfo) user_signal(SIGSEGV, regs, pid); else handle_segv(pid, regs); break; case SIGTRAP + 0x80: handle_trap(pid, regs, local_using_sysemu);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -