minix/servers/pm/misc.c
Ben Gras 7967177710 endpoint-aware conversion of servers.
'who', indicating caller number in pm and fs and some other servers, has
been removed in favour of 'who_e' (endpoint) and 'who_p' (proc nr.).

In both PM and FS, isokendpt() convert endpoints to process slot
numbers, returning OK if it was a valid and consistent endpoint number.
okendpt() does the same but panic()s if it doesn't succeed. (In PM,
this is pm_isok..)

pm and fs keep their own records of process endpoints in their proc tables,
which are needed to make kernel calls about those processes.

message field names have changed.

fs drivers are endpoints.

fs now doesn't try to get out of driver deadlock, as the protocol isn't
supposed to let that happen any more. (A warning is printed if ELOCKED
is detected though.)

fproc[].fp_task (indicating which driver the process is suspended on)
became an int.

PM and FS now get endpoint numbers of initial boot processes from the
kernel. These happen to be the same as the old proc numbers, to let
user processes reach them with the old numbers, but FS and PM don't know
that. All new processes after INIT, even after the generation number
wraps around, get endpoint numbers with generation 1 and higher, so
the first instances of the boot processes are the only processes ever
to have endpoint numbers in the old proc number range.

More return code checks of sys_* functions have been added.

IS has become endpoint-aware. Ditched the 'text' and 'data' fields
in the kernel dump (which show locations, not sizes, so aren't terribly
useful) in favour of the endpoint number. Proc number is still visible.

Some other dumps (e.g. dmap, rs) show endpoint numbers now too which got
the formatting changed.

PM reading segments using rw_seg() has changed - it uses other fields
in the message now instead of encoding the segment and process number and
fd in the fd field. For that it uses _read_pm() and _write_pm() which to
_taskcall()s directly in pm/misc.c.

PM now sys_exit()s itself on panic(), instead of sys_abort().

RS also talks in endpoints instead of process numbers.
2006-03-03 10:20:58 +00:00

407 lines
12 KiB
C

/* Miscellaneous system calls. Author: Kees J. Bot
* 31 Mar 2000
* The entry points into this file are:
* do_reboot: kill all processes, then reboot system
* do_svrctl: process manager control
* do_getsysinfo: request copy of PM data structure (Jorrit N. Herder)
* do_getprocnr: lookup process slot number (Jorrit N. Herder)
* do_memalloc: allocate a chunk of memory (Jorrit N. Herder)
* do_memfree: deallocate a chunk of memory (Jorrit N. Herder)
* do_getsetpriority: get/set process priority
*/
#include "pm.h"
#include <minix/callnr.h>
#include <signal.h>
#include <sys/svrctl.h>
#include <sys/resource.h>
#include <minix/com.h>
#include <minix/config.h>
#include <minix/type.h>
#include <string.h>
#include <lib.h>
#include "mproc.h"
#include "param.h"
/*===========================================================================*
* do_allocmem *
*===========================================================================*/
PUBLIC int do_allocmem()
{
vir_clicks mem_clicks;
phys_clicks mem_base;
mem_clicks = (m_in.memsize + CLICK_SIZE -1 ) >> CLICK_SHIFT;
mem_base = alloc_mem(mem_clicks);
if (mem_base == NO_MEM) return(ENOMEM);
mp->mp_reply.membase = (phys_bytes) (mem_base << CLICK_SHIFT);
return(OK);
}
/*===========================================================================*
* do_freemem *
*===========================================================================*/
PUBLIC int do_freemem()
{
vir_clicks mem_clicks;
phys_clicks mem_base;
mem_clicks = (m_in.memsize + CLICK_SIZE -1 ) >> CLICK_SHIFT;
mem_base = (m_in.membase + CLICK_SIZE -1 ) >> CLICK_SHIFT;
free_mem(mem_base, mem_clicks);
return(OK);
}
/*===========================================================================*
* do_getsysinfo *
*===========================================================================*/
PUBLIC int do_getsysinfo()
{
struct mproc *proc_addr;
vir_bytes src_addr, dst_addr;
struct kinfo kinfo;
struct loadinfo loadinfo;
size_t len;
static struct pm_mem_info pmi;
int s, r;
size_t holesize;
switch(m_in.info_what) {
case SI_KINFO: /* kernel info is obtained via PM */
sys_getkinfo(&kinfo);
src_addr = (vir_bytes) &kinfo;
len = sizeof(struct kinfo);
break;
case SI_PROC_ADDR: /* get address of PM process table */
proc_addr = &mproc[0];
src_addr = (vir_bytes) &proc_addr;
len = sizeof(struct mproc *);
break;
case SI_PROC_TAB: /* copy entire process table */
src_addr = (vir_bytes) mproc;
len = sizeof(struct mproc) * NR_PROCS;
break;
case SI_MEM_ALLOC:
holesize = sizeof(pmi.pmi_holes);
if((r=mem_holes_copy(pmi.pmi_holes, &holesize,
&pmi.pmi_hi_watermark)) != OK)
return r;
src_addr = (vir_bytes) &pmi;
len = sizeof(pmi);
break;
case SI_LOADINFO: /* loadinfo is obtained via PM */
sys_getloadinfo(&loadinfo);
src_addr = (vir_bytes) &loadinfo;
len = sizeof(struct loadinfo);
break;
default:
return(EINVAL);
}
dst_addr = (vir_bytes) m_in.info_where;
if (OK != (s=sys_datacopy(SELF, src_addr, who_e, dst_addr, len)))
return(s);
return(OK);
}
/*===========================================================================*
* do_getprocnr *
*===========================================================================*/
PUBLIC int do_getprocnr()
{
register struct mproc *rmp;
static char search_key[PROC_NAME_LEN+1];
int key_len;
int s;
if (m_in.pid >= 0) { /* lookup process by pid */
for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++) {
if ((rmp->mp_flags & IN_USE) && (rmp->mp_pid==m_in.pid)) {
mp->mp_reply.endpt = rmp->mp_endpoint;
return(OK);
}
}
return(ESRCH);
} else if (m_in.namelen > 0) { /* lookup process by name */
key_len = MIN(m_in.namelen, PROC_NAME_LEN);
if (OK != (s=sys_datacopy(who_e, (vir_bytes) m_in.addr,
SELF, (vir_bytes) search_key, key_len)))
return(s);
search_key[key_len] = '\0'; /* terminate for safety */
for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++) {
if (((rmp->mp_flags & (IN_USE | ZOMBIE)) == IN_USE) &&
strncmp(rmp->mp_name, search_key, key_len)==0) {
mp->mp_reply.endpt = rmp->mp_endpoint;
return(OK);
}
}
return(ESRCH);
} else { /* return own/parent process number */
mp->mp_reply.endpt = who_e;
mp->mp_reply.pendpt = mproc[mp->mp_parent].mp_endpoint;
}
return(OK);
}
/*===========================================================================*
* do_reboot *
*===========================================================================*/
#define REBOOT_CODE "delay; boot"
PUBLIC int do_reboot()
{
char monitor_code[32*sizeof(char *)];
int code_len;
int abort_flag;
if (mp->mp_effuid != SUPER_USER) return(EPERM);
switch (m_in.reboot_flag) {
case RBT_HALT:
case RBT_PANIC:
case RBT_RESET:
abort_flag = m_in.reboot_flag;
break;
case RBT_REBOOT:
code_len = strlen(REBOOT_CODE) + 1;
strncpy(monitor_code, REBOOT_CODE, code_len);
abort_flag = RBT_MONITOR;
break;
case RBT_MONITOR:
code_len = m_in.reboot_strlen + 1;
if (code_len > sizeof(monitor_code)) return(EINVAL);
if (sys_datacopy(who_e, (vir_bytes) m_in.reboot_code,
PM_PROC_NR, (vir_bytes) monitor_code,
(phys_bytes) (code_len)) != OK) return(EFAULT);
if (monitor_code[code_len-1] != 0) return(EINVAL);
abort_flag = RBT_MONITOR;
break;
default:
return(EINVAL);
}
/* Order matters here. When FS is told to reboot, it exits all its
* processes, and then would be confused if they're exited again by
* SIGKILL. So first kill, then reboot.
*/
check_sig(-1, SIGKILL); /* kill all processes except init */
tell_fs(REBOOT, 0, 0, 0); /* tell FS to synchronize */
/* Ask the kernel to abort. All system services, including the PM, will
* get a HARD_STOP notification. Await the notification in the main loop.
*/
sys_abort(abort_flag, PM_PROC_NR, monitor_code, code_len);
return(SUSPEND); /* don't reply to killed process */
}
/*===========================================================================*
* do_getsetpriority *
*===========================================================================*/
PUBLIC int do_getsetpriority()
{
int arg_which, arg_who, arg_pri;
int rmp_nr;
struct mproc *rmp;
arg_which = m_in.m1_i1;
arg_who = m_in.m1_i2;
arg_pri = m_in.m1_i3; /* for SETPRIORITY */
/* Code common to GETPRIORITY and SETPRIORITY. */
/* Only support PRIO_PROCESS for now. */
if (arg_which != PRIO_PROCESS)
return(EINVAL);
if (arg_who == 0)
rmp_nr = who_p;
else
if ((rmp_nr = proc_from_pid(arg_who)) < 0)
return(ESRCH);
rmp = &mproc[rmp_nr];
if (mp->mp_effuid != SUPER_USER &&
mp->mp_effuid != rmp->mp_effuid && mp->mp_effuid != rmp->mp_realuid)
return EPERM;
/* If GET, that's it. */
if (call_nr == GETPRIORITY) {
return(rmp->mp_nice - PRIO_MIN);
}
/* Only root is allowed to reduce the nice level. */
if (rmp->mp_nice > arg_pri && mp->mp_effuid != SUPER_USER)
return(EACCES);
/* We're SET, and it's allowed. Do it and tell kernel. */
rmp->mp_nice = arg_pri;
return sys_nice(rmp_nr, arg_pri);
}
/*===========================================================================*
* do_svrctl *
*===========================================================================*/
PUBLIC int do_svrctl()
{
int s, req;
vir_bytes ptr;
#define MAX_LOCAL_PARAMS 2
static struct {
char name[30];
char value[30];
} local_param_overrides[MAX_LOCAL_PARAMS];
static int local_params = 0;
req = m_in.svrctl_req;
ptr = (vir_bytes) m_in.svrctl_argp;
/* Is the request indeed for the MM? */
if (((req >> 8) & 0xFF) != 'M') return(EINVAL);
/* Control operations local to the PM. */
switch(req) {
case MMSETPARAM:
case MMGETPARAM: {
struct sysgetenv sysgetenv;
char search_key[64];
char *val_start;
size_t val_len;
size_t copy_len;
/* Copy sysgetenv structure to PM. */
if (sys_datacopy(who_e, ptr, SELF, (vir_bytes) &sysgetenv,
sizeof(sysgetenv)) != OK) return(EFAULT);
/* Set a param override? */
if (req == MMSETPARAM) {
if (local_params >= MAX_LOCAL_PARAMS) return ENOSPC;
if (sysgetenv.keylen <= 0
|| sysgetenv.keylen >=
sizeof(local_param_overrides[local_params].name)
|| sysgetenv.vallen <= 0
|| sysgetenv.vallen >=
sizeof(local_param_overrides[local_params].value))
return EINVAL;
if ((s = sys_datacopy(who_e, (vir_bytes) sysgetenv.key,
SELF, (vir_bytes) local_param_overrides[local_params].name,
sysgetenv.keylen)) != OK)
return s;
if ((s = sys_datacopy(who_e, (vir_bytes) sysgetenv.val,
SELF, (vir_bytes) local_param_overrides[local_params].value,
sysgetenv.keylen)) != OK)
return s;
local_param_overrides[local_params].name[sysgetenv.keylen] = '\0';
local_param_overrides[local_params].value[sysgetenv.vallen] = '\0';
local_params++;
return OK;
}
if (sysgetenv.keylen == 0) { /* copy all parameters */
val_start = monitor_params;
val_len = sizeof(monitor_params);
}
else { /* lookup value for key */
int p;
/* Try to get a copy of the requested key. */
if (sysgetenv.keylen > sizeof(search_key)) return(EINVAL);
if ((s = sys_datacopy(who_e, (vir_bytes) sysgetenv.key,
SELF, (vir_bytes) search_key, sysgetenv.keylen)) != OK)
return(s);
/* Make sure key is null-terminated and lookup value.
* First check local overrides.
*/
search_key[sysgetenv.keylen-1]= '\0';
for(p = 0; p < local_params; p++) {
if (!strcmp(search_key, local_param_overrides[p].name)) {
val_start = local_param_overrides[p].value;
break;
}
}
if (p >= local_params && (val_start = find_param(search_key)) == NULL)
return(ESRCH);
val_len = strlen(val_start) + 1;
}
/* See if it fits in the client's buffer. */
if (val_len > sysgetenv.vallen)
return E2BIG;
/* Value found, make the actual copy (as far as possible). */
copy_len = MIN(val_len, sysgetenv.vallen);
if ((s=sys_datacopy(SELF, (vir_bytes) val_start,
who_e, (vir_bytes) sysgetenv.val, copy_len)) != OK)
return(s);
return OK;
}
#if ENABLE_SWAP
case MMSWAPON: {
struct mmswapon swapon;
if (mp->mp_effuid != SUPER_USER) return(EPERM);
if (sys_datacopy(who_e, (phys_bytes) ptr,
PM_PROC_NR, (phys_bytes) &swapon,
(phys_bytes) sizeof(swapon)) != OK) return(EFAULT);
return(swap_on(swapon.file, swapon.offset, swapon.size)); }
case MMSWAPOFF: {
if (mp->mp_effuid != SUPER_USER) return(EPERM);
return(swap_off()); }
#endif /* SWAP */
default:
return(EINVAL);
}
}
/*===========================================================================*
* _read_pm *
*===========================================================================*/
PUBLIC ssize_t _read_pm(fd, buffer, nbytes, seg, ep)
int fd;
void *buffer;
size_t nbytes;
int seg;
int ep;
{
message m;
m.m1_i1 = _PM_SEG_FLAG | fd;
m.m1_i2 = nbytes;
m.m1_p1 = (char *) buffer;
m.m1_p2 = (char *) seg;
m.m1_p3 = (char *) ep;
return(_syscall(FS_PROC_NR, READ, &m));
}
/*===========================================================================*
* _write_pm *
*===========================================================================*/
PUBLIC ssize_t _write_pm(fd, buffer, nbytes, seg, ep)
int fd;
void *buffer;
size_t nbytes;
int seg;
int ep;
{
message m;
m.m1_i1 = _PM_SEG_FLAG | fd;
m.m1_i2 = nbytes;
m.m1_p1 = (char *) buffer;
m.m1_p2 = (char *) seg;
m.m1_p3 = (char *) ep;
return(_syscall(FS_PROC_NR, WRITE, &m));
}