50e2064049
This commit removes all traces of Minix segments (the text/data/stack memory map abstraction in the kernel) and significance of Intel segments (hardware segments like CS, DS that add offsets to all addressing before page table translation). This ultimately simplifies the memory layout and addressing and makes the same layout possible on non-Intel architectures. There are only two types of addresses in the world now: virtual and physical; even the kernel and processes have the same virtual address space. Kernel and user processes can be distinguished at a glance as processes won't use 0xF0000000 and above. No static pre-allocated memory sizes exist any more. Changes to booting: . The pre_init.c leaves the kernel and modules exactly as they were left by the bootloader in physical memory . The kernel starts running using physical addressing, loaded at a fixed location given in its linker script by the bootloader. All code and data in this phase are linked to this fixed low location. . It makes a bootstrap pagetable to map itself to a fixed high location (also in linker script) and jumps to the high address. All code and data then use this high addressing. . All code/data symbols linked at the low addresses is prefixed by an objcopy step with __k_unpaged_*, so that that code cannot reference highly-linked symbols (which aren't valid yet) or vice versa (symbols that aren't valid any more). . The two addressing modes are separated in the linker script by collecting the unpaged_*.o objects and linking them with low addresses, and linking the rest high. Some objects are linked twice, once low and once high. . The bootstrap phase passes a lot of information (e.g. free memory list, physical location of the modules, etc.) using the kinfo struct. . After this bootstrap the low-linked part is freed. . The kernel maps in VM into the bootstrap page table so that VM can begin executing. Its first job is to make page tables for all other boot processes. So VM runs before RS, and RS gets a fully dynamic, VM-managed address space. VM gets its privilege info from RS as usual but that happens after RS starts running. . Both the kernel loading VM and VM organizing boot processes happen using the libexec logic. This removes the last reason for VM to still know much about exec() and vm/exec.c is gone. Further Implementation: . All segments are based at 0 and have a 4 GB limit. . The kernel is mapped in at the top of the virtual address space so as not to constrain the user processes. . Processes do not use segments from the LDT at all; there are no segments in the LDT any more, so no LLDT is needed. . The Minix segments T/D/S are gone and so none of the user-space or in-kernel copy functions use them. The copy functions use a process endpoint of NONE to realize it's a physical address, virtual otherwise. . The umap call only makes sense to translate a virtual address to a physical address now. . Segments-related calls like newmap and alloc_segments are gone. . All segments-related translation in VM is gone (vir2map etc). . Initialization in VM is simpler as no moving around is necessary. . VM and all other boot processes can be linked wherever they wish and will be mapped in at the right location by the kernel and VM respectively. Other changes: . The multiboot code is less special: it does not use mb_print for its diagnostics any more but uses printf() as normal, saving the output into the diagnostics buffer, only printing to the screen using the direct print functions if a panic() occurs. . The multiboot code uses the flexible 'free memory map list' style to receive the list of free memory if available. . The kernel determines the memory layout of the processes to a degree: it tells VM where the kernel starts and ends and where the kernel wants the top of the process to be. VM then uses this entire range, i.e. the stack is right at the top, and mmap()ped bits of memory are placed below that downwards, and the break grows upwards. Other Consequences: . Every process gets its own page table as address spaces can't be separated any more by segments. . As all segments are 0-based, there is no distinction between virtual and linear addresses, nor between userspace and kernel addresses. . Less work is done when context switching, leading to a net performance increase. (8% faster on my machine for 'make servers'.) . The layout and configuration of the GDT makes sysenter and syscall possible.
206 lines
5.7 KiB
C
206 lines
5.7 KiB
C
/* This file handles the EXEC system call. It performs the work as follows:
|
|
* - see if the permissions allow the file to be executed
|
|
* - read the header and extract the sizes
|
|
* - fetch the initial args and environment from the user space
|
|
* - allocate the memory for the new process
|
|
* - copy the initial stack from PM to the process
|
|
* - read in the text and data segments and copy to the process
|
|
* - take care of setuid and setgid bits
|
|
* - fix up 'mproc' table
|
|
* - tell kernel about EXEC
|
|
* - save offset to initial argc (for procfs)
|
|
*
|
|
* The entry points into this file are:
|
|
* do_exec: perform the EXEC system call
|
|
* do_newexec: handle PM part of exec call after VFS
|
|
* do_execrestart: finish the special exec call for RS
|
|
* exec_restart: finish a regular exec call
|
|
*/
|
|
|
|
#include "pm.h"
|
|
#include <sys/stat.h>
|
|
#include <minix/callnr.h>
|
|
#include <minix/endpoint.h>
|
|
#include <minix/com.h>
|
|
#include <minix/vm.h>
|
|
#include <a.out.h>
|
|
#include <signal.h>
|
|
#include <string.h>
|
|
#include <libexec.h>
|
|
#include <sys/ptrace.h>
|
|
#include "mproc.h"
|
|
#include "param.h"
|
|
|
|
#define ESCRIPT (-2000) /* Returned by read_header for a #! script. */
|
|
#define PTRSIZE sizeof(char *) /* Size of pointers in argv[] and envp[]. */
|
|
|
|
/*===========================================================================*
|
|
* do_exec *
|
|
*===========================================================================*/
|
|
int do_exec()
|
|
{
|
|
message m;
|
|
|
|
/* Forward call to VFS */
|
|
m.m_type = PM_EXEC;
|
|
m.PM_PROC = mp->mp_endpoint;
|
|
m.PM_PATH = m_in.exec_name;
|
|
m.PM_PATH_LEN = m_in.exec_len;
|
|
m.PM_FRAME = m_in.frame_ptr;
|
|
m.PM_FRAME_LEN = m_in.msg_frame_len;
|
|
m.PM_EXECFLAGS = m_in.PMEXEC_FLAGS;
|
|
|
|
tell_vfs(mp, &m);
|
|
|
|
/* Do not reply */
|
|
return SUSPEND;
|
|
}
|
|
|
|
|
|
/*===========================================================================*
|
|
* do_newexec *
|
|
*===========================================================================*/
|
|
int do_newexec()
|
|
{
|
|
int proc_e, proc_n, allow_setuid;
|
|
char *ptr;
|
|
struct mproc *rmp;
|
|
struct exec_info args;
|
|
int r, flags;
|
|
|
|
if (who_e != VFS_PROC_NR && who_e != RS_PROC_NR)
|
|
return EPERM;
|
|
|
|
proc_e= m_in.EXC_NM_PROC;
|
|
if (pm_isokendpt(proc_e, &proc_n) != OK) {
|
|
panic("do_newexec: got bad endpoint: %d", proc_e);
|
|
}
|
|
rmp= &mproc[proc_n];
|
|
ptr= m_in.EXC_NM_PTR;
|
|
r= sys_datacopy(who_e, (vir_bytes)ptr,
|
|
SELF, (vir_bytes)&args, sizeof(args));
|
|
if (r != OK)
|
|
panic("do_newexec: sys_datacopy failed: %d", r);
|
|
|
|
allow_setuid = 0; /* Do not allow setuid execution */
|
|
rmp->mp_flags &= ~TAINTED; /* By default not tainted */
|
|
|
|
if (rmp->mp_tracer == NO_TRACER) {
|
|
/* Okay, setuid execution is allowed */
|
|
allow_setuid = 1;
|
|
}
|
|
|
|
if (allow_setuid && args.allow_setuid) {
|
|
rmp->mp_effuid = args.new_uid;
|
|
rmp->mp_effgid = args.new_gid;
|
|
}
|
|
|
|
/* A process is considered 'tainted' when it's executing with
|
|
* setuid or setgid bit set, or when the real{u,g}id doesn't
|
|
* match the eff{u,g}id, respectively. */
|
|
if (allow_setuid && args.allow_setuid) {
|
|
/* Program has setuid and/or setgid bits set */
|
|
rmp->mp_flags |= TAINTED;
|
|
} else if (rmp->mp_effuid != rmp->mp_realuid ||
|
|
rmp->mp_effgid != rmp->mp_realgid) {
|
|
rmp->mp_flags |= TAINTED;
|
|
}
|
|
|
|
/* System will save command line for debugging, ps(1) output, etc. */
|
|
strncpy(rmp->mp_name, args.progname, PROC_NAME_LEN-1);
|
|
rmp->mp_name[PROC_NAME_LEN-1] = '\0';
|
|
|
|
/* Save offset to initial argc (for procfs) */
|
|
rmp->mp_frame_addr = (vir_bytes) args.stack_high - args.frame_len;
|
|
rmp->mp_frame_len = args.frame_len;
|
|
|
|
/* Kill process if something goes wrong after this point. */
|
|
rmp->mp_flags |= PARTIAL_EXEC;
|
|
|
|
mp->mp_reply.reply_res2= (vir_bytes) rmp->mp_frame_addr;
|
|
mp->mp_reply.reply_res3= flags;
|
|
if (allow_setuid && args.allow_setuid)
|
|
mp->mp_reply.reply_res3 |= EXC_NM_RF_ALLOW_SETUID;
|
|
|
|
return r;
|
|
}
|
|
|
|
/*===========================================================================*
|
|
* do_execrestart *
|
|
*===========================================================================*/
|
|
int do_execrestart()
|
|
{
|
|
int proc_e, proc_n, result;
|
|
struct mproc *rmp;
|
|
vir_bytes pc;
|
|
|
|
if (who_e != RS_PROC_NR)
|
|
return EPERM;
|
|
|
|
proc_e= m_in.EXC_RS_PROC;
|
|
if (pm_isokendpt(proc_e, &proc_n) != OK) {
|
|
panic("do_execrestart: got bad endpoint: %d", proc_e);
|
|
}
|
|
rmp= &mproc[proc_n];
|
|
result= m_in.EXC_RS_RESULT;
|
|
pc= (vir_bytes)m_in.EXC_RS_PC;
|
|
|
|
exec_restart(rmp, result, pc, rmp->mp_frame_addr);
|
|
|
|
return OK;
|
|
}
|
|
|
|
|
|
/*===========================================================================*
|
|
* exec_restart *
|
|
*===========================================================================*/
|
|
void exec_restart(rmp, result, pc, vfs_newsp)
|
|
struct mproc *rmp;
|
|
int result;
|
|
vir_bytes pc;
|
|
vir_bytes vfs_newsp;
|
|
{
|
|
int r, sn;
|
|
|
|
if (result != OK)
|
|
{
|
|
if (rmp->mp_flags & PARTIAL_EXEC)
|
|
{
|
|
/* Use SIGKILL to signal that something went wrong */
|
|
sys_kill(rmp->mp_endpoint, SIGKILL);
|
|
return;
|
|
}
|
|
setreply(rmp-mproc, result);
|
|
return;
|
|
}
|
|
|
|
rmp->mp_flags &= ~PARTIAL_EXEC;
|
|
|
|
/* Fix 'mproc' fields, tell kernel that exec is done, reset caught
|
|
* sigs.
|
|
*/
|
|
for (sn = 1; sn < _NSIG; sn++) {
|
|
if (sigismember(&rmp->mp_catch, sn)) {
|
|
sigdelset(&rmp->mp_catch, sn);
|
|
rmp->mp_sigact[sn].sa_handler = SIG_DFL;
|
|
sigemptyset(&rmp->mp_sigact[sn].sa_mask);
|
|
}
|
|
}
|
|
|
|
/* Cause a signal if this process is traced.
|
|
* Do this before making the process runnable again!
|
|
*/
|
|
#if USE_TRACE
|
|
if (rmp->mp_tracer != NO_TRACER && !(rmp->mp_trace_flags & TO_NOEXEC))
|
|
{
|
|
sn = (rmp->mp_trace_flags & TO_ALTEXEC) ? SIGSTOP : SIGTRAP;
|
|
|
|
check_sig(rmp->mp_pid, sn, FALSE /* ksig */);
|
|
}
|
|
#endif /* USE_TRACE */
|
|
|
|
/* Call kernel to exec with SP and PC set by VFS. */
|
|
r= sys_exec(rmp->mp_endpoint, (char *) vfs_newsp, rmp->mp_name, pc);
|
|
if (r != OK) panic("sys_exec failed: %d", r);
|
|
}
|
|
|