2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#define _SYSTEM 1
|
|
|
|
|
|
|
|
#include <minix/callnr.h>
|
|
|
|
#include <minix/com.h>
|
|
|
|
#include <minix/config.h>
|
|
|
|
#include <minix/const.h>
|
|
|
|
#include <minix/ds.h>
|
|
|
|
#include <minix/endpoint.h>
|
|
|
|
#include <minix/keymap.h>
|
|
|
|
#include <minix/minlib.h>
|
|
|
|
#include <minix/type.h>
|
|
|
|
#include <minix/ipc.h>
|
|
|
|
#include <minix/sysutil.h>
|
|
|
|
#include <minix/syslib.h>
|
2009-09-21 16:49:49 +02:00
|
|
|
#include <minix/debug.h>
|
|
|
|
#include <minix/bitmap.h>
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
#include <string.h>
|
2008-11-19 13:26:10 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <env.h>
|
2010-03-18 18:17:31 +01:00
|
|
|
#include <assert.h>
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#include "glo.h"
|
|
|
|
#include "vm.h"
|
|
|
|
#include "proto.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "sanitycheck.h"
|
|
|
|
#include "region.h"
|
2010-03-18 18:17:31 +01:00
|
|
|
#include "memory.h"
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* do_fork *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int do_fork(message *msg)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
2011-06-01 11:30:58 +02:00
|
|
|
int r, proc, childproc, fullvm;
|
2008-11-19 13:26:10 +01:00
|
|
|
struct vmproc *vmp, *vmc;
|
2009-09-21 16:49:49 +02:00
|
|
|
pt_t origpt;
|
|
|
|
vir_bytes msgaddr;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
if(vm_isokendpt(msg->VMF_ENDPOINT, &proc) != OK) {
|
|
|
|
printf("VM: bogus endpoint VM_FORK %d\n", msg->VMF_ENDPOINT);
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
childproc = msg->VMF_SLOTNO;
|
|
|
|
if(childproc < 0 || childproc >= NR_PROCS) {
|
|
|
|
printf("VM: bogus slotno VM_FORK %d\n", msg->VMF_SLOTNO);
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
vmp = &vmproc[proc]; /* parent */
|
|
|
|
vmc = &vmproc[childproc]; /* child */
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(vmc->vm_slot == childproc);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
if(vmp->vm_flags & VMF_HAS_DMA) {
|
|
|
|
printf("VM: %d has DMA memory and may not fork\n", msg->VMF_ENDPOINT);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fullvm = vmp->vm_flags & VMF_HASPT;
|
|
|
|
|
|
|
|
/* The child is basically a copy of the parent. */
|
2009-09-21 16:49:49 +02:00
|
|
|
origpt = vmc->vm_pt;
|
2008-11-19 13:26:10 +01:00
|
|
|
*vmc = *vmp;
|
2009-09-21 16:49:49 +02:00
|
|
|
vmc->vm_slot = childproc;
|
2010-10-04 13:41:10 +02:00
|
|
|
region_init(&vmc->vm_regions_avl);
|
2008-11-19 13:26:10 +01:00
|
|
|
vmc->vm_endpoint = NONE; /* In case someone tries to use it. */
|
2009-09-21 16:49:49 +02:00
|
|
|
vmc->vm_pt = origpt;
|
2010-04-12 13:25:24 +02:00
|
|
|
vmc->vm_flags &= ~VMF_HASPT;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#if VMSTATS
|
|
|
|
vmc->vm_bytecopies = 0;
|
|
|
|
#endif
|
|
|
|
|
2010-03-18 18:17:31 +01:00
|
|
|
if(pt_new(&vmc->vm_pt) != OK) {
|
|
|
|
printf("VM: fork: pt_new failed\n");
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
vmc->vm_flags |= VMF_HASPT;
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
if(fullvm) {
|
|
|
|
SANITYCHECK(SCL_DETAIL);
|
|
|
|
|
|
|
|
if(map_proc_copy(vmc, vmp) != OK) {
|
|
|
|
printf("VM: fork: map_proc_copy failed\n");
|
|
|
|
pt_free(&vmc->vm_pt);
|
|
|
|
return(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(vmp->vm_heap) {
|
|
|
|
vmc->vm_heap = map_region_lookup_tag(vmc, VRT_HEAP);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(vmc->vm_heap);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_DETAIL);
|
|
|
|
} else {
|
2010-03-18 18:17:31 +01:00
|
|
|
vir_bytes sp;
|
2010-04-12 13:25:24 +02:00
|
|
|
struct vir_region *heap, *stack;
|
2010-03-18 18:17:31 +01:00
|
|
|
vir_bytes text_bytes, data_bytes, stack_bytes, parent_gap_bytes,
|
|
|
|
child_gap_bytes;
|
2011-02-27 00:00:55 +01:00
|
|
|
vir_bytes text_addr, data_addr;
|
|
|
|
int is_elf = 0;
|
2010-03-18 18:17:31 +01:00
|
|
|
|
|
|
|
/* Get SP of new process (using parent). */
|
|
|
|
if(get_stack_ptr(vmp->vm_endpoint, &sp) != OK) {
|
|
|
|
printf("VM: fork: get_stack_ptr failed for %d\n",
|
|
|
|
vmp->vm_endpoint);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update size of stack segment using current SP. */
|
|
|
|
if(adjust(vmp, vmp->vm_arch.vm_seg[D].mem_len, sp) != OK) {
|
|
|
|
printf("VM: fork: adjust failed for %d\n",
|
|
|
|
vmp->vm_endpoint);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-03-18 18:17:31 +01:00
|
|
|
/* Copy newly adjust()ed stack segment size to child. */
|
|
|
|
vmc->vm_arch.vm_seg[S] = vmp->vm_arch.vm_seg[S];
|
|
|
|
|
2011-02-27 00:00:55 +01:00
|
|
|
text_addr = CLICK2ABS(vmc->vm_arch.vm_seg[T].mem_vir);
|
2010-03-18 18:17:31 +01:00
|
|
|
text_bytes = CLICK2ABS(vmc->vm_arch.vm_seg[T].mem_len);
|
2011-02-27 00:00:55 +01:00
|
|
|
data_addr = CLICK2ABS(vmc->vm_arch.vm_seg[D].mem_vir);
|
2010-03-18 18:17:31 +01:00
|
|
|
data_bytes = CLICK2ABS(vmc->vm_arch.vm_seg[D].mem_len);
|
|
|
|
stack_bytes = CLICK2ABS(vmc->vm_arch.vm_seg[S].mem_len);
|
|
|
|
|
|
|
|
/* how much space after break and before lower end (which is the
|
|
|
|
* logical top) of stack for the parent
|
2008-11-19 13:26:10 +01:00
|
|
|
*/
|
2010-03-18 18:17:31 +01:00
|
|
|
parent_gap_bytes = CLICK2ABS(vmc->vm_arch.vm_seg[S].mem_vir -
|
2011-02-27 00:00:55 +01:00
|
|
|
vmc->vm_arch.vm_seg[D].mem_len -
|
|
|
|
vmc->vm_arch.vm_seg[D].mem_vir);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-03-18 18:17:31 +01:00
|
|
|
/* how much space can the child stack grow downwards, below
|
|
|
|
* the current SP? The rest of the gap is available for the
|
|
|
|
* heap to grow upwards.
|
|
|
|
*/
|
|
|
|
child_gap_bytes = VM_PAGE_SIZE;
|
|
|
|
|
2011-02-27 00:00:55 +01:00
|
|
|
#if defined(__ELF__)
|
|
|
|
is_elf = 1;
|
|
|
|
#endif
|
|
|
|
|
2010-03-18 18:17:31 +01:00
|
|
|
if((r=proc_new(vmc, VM_PROCSTART,
|
2011-02-27 00:00:55 +01:00
|
|
|
text_addr, text_bytes,
|
|
|
|
data_addr, data_bytes,
|
|
|
|
stack_bytes, child_gap_bytes, 0, 0,
|
2010-03-18 18:17:31 +01:00
|
|
|
CLICK2ABS(vmc->vm_arch.vm_seg[S].mem_vir +
|
2011-02-27 00:00:55 +01:00
|
|
|
vmc->vm_arch.vm_seg[S].mem_len),
|
2012-04-05 03:37:17 +02:00
|
|
|
1, is_elf, 0)) != OK) {
|
2010-03-18 18:17:31 +01:00
|
|
|
printf("VM: fork: proc_new failed\n");
|
|
|
|
return r;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
if(!(heap = map_region_lookup_tag(vmc, VRT_HEAP)))
|
|
|
|
panic("couldn't lookup heap");
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(heap->phys);
|
2010-04-12 13:25:24 +02:00
|
|
|
if(!(stack = map_region_lookup_tag(vmc, VRT_STACK)))
|
2010-03-18 18:17:31 +01:00
|
|
|
panic("couldn't lookup stack");
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(stack->phys);
|
2010-03-18 18:17:31 +01:00
|
|
|
|
|
|
|
/* Now copy the memory regions. */
|
|
|
|
|
|
|
|
if(vmc->vm_arch.vm_seg[T].mem_len > 0) {
|
2010-04-12 13:25:24 +02:00
|
|
|
struct vir_region *text;
|
|
|
|
if(!(text = map_region_lookup_tag(vmc, VRT_TEXT)))
|
2010-03-18 18:17:31 +01:00
|
|
|
panic("couldn't lookup text");
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(text->phys);
|
2010-04-12 13:25:24 +02:00
|
|
|
if(copy_abs2region(CLICK2ABS(vmp->vm_arch.vm_seg[T].mem_phys),
|
|
|
|
text, 0, text_bytes) != OK)
|
2010-03-18 18:17:31 +01:00
|
|
|
panic("couldn't copy text");
|
2009-12-07 13:10:44 +01:00
|
|
|
}
|
2010-03-18 18:17:31 +01:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
if(copy_abs2region(CLICK2ABS(vmp->vm_arch.vm_seg[D].mem_phys),
|
|
|
|
heap, 0, data_bytes) != OK)
|
|
|
|
panic("couldn't copy heap");
|
2010-03-18 18:17:31 +01:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
if(copy_abs2region(CLICK2ABS(vmp->vm_arch.vm_seg[D].mem_phys +
|
2010-03-18 18:17:31 +01:00
|
|
|
vmc->vm_arch.vm_seg[D].mem_len) + parent_gap_bytes,
|
2010-04-12 13:25:24 +02:00
|
|
|
stack, child_gap_bytes, stack_bytes) != OK)
|
2010-03-18 18:17:31 +01:00
|
|
|
panic("couldn't copy stack");
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Only inherit these flags. */
|
|
|
|
vmc->vm_flags &= (VMF_INUSE|VMF_SEPARATE|VMF_HASPT);
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
/* inherit the priv call bitmaps */
|
Initialization protocol for system services.
SYSLIB CHANGES:
- SEF framework now supports a new SEF Init request type from RS. 3 different
callbacks are available (init_fresh, init_lu, init_restart) to specify
initialization code when a service starts fresh, starts after a live update,
or restarts.
SYSTEM SERVICE CHANGES:
- Initialization code for system services is now enclosed in a callback SEF will
automatically call at init time. The return code of the callback will
tell RS whether the initialization completed successfully.
- Each init callback can access information passed by RS to initialize. As of
now, each system service has access to the public entries of RS's system process
table to gather all the information required to initialize. This design
eliminates many existing or potential races at boot time and provides a uniform
initialization interface to system services. The same interface will be reused
for the upcoming publish/subscribe model to handle dynamic
registration / deregistration of system services.
VM CHANGES:
- Uniform privilege management for all system services. Every service uses the
same call mask format. For boot services, VM copies the call mask from init
data. For dynamic services, VM still receives the call mask via rs_set_priv
call that will be soon replaced by the upcoming publish/subscribe model.
RS CHANGES:
- The system process table has been reorganized and split into private entries
and public entries. Only the latter ones are exposed to system services.
- VM call masks are now entirely configured in rs/table.c
- RS has now its own slot in the system process table. Only kernel tasks and
user processes not included in the boot image are now left out from the system
process table.
- RS implements the initialization protocol for system services.
- For services in the boot image, RS blocks till initialization is complete and
panics when failure is reported back. Services are initialized in their order of
appearance in the boot image priv table and RS blocks to implements synchronous
initialization for every system service having the flag SF_SYNCH_BOOT set.
- For services started dynamically, the initialization protocol is implemented
as though it were the first ping for the service. In this case, if the
system service fails to report back (or reports failure), RS brings the service
down rather than trying to restart it.
2010-01-08 02:20:42 +01:00
|
|
|
memcpy(&vmc->vm_call_mask, &vmp->vm_call_mask, sizeof(vmc->vm_call_mask));
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/* Tell kernel about the (now successful) FORK. */
|
|
|
|
if((r=sys_fork(vmp->vm_endpoint, childproc,
|
|
|
|
&vmc->vm_endpoint, vmc->vm_arch.vm_seg,
|
2009-12-07 13:10:44 +01:00
|
|
|
PFF_VMINHIBIT, &msgaddr)) != OK) {
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("do_fork can't sys_fork: %d", r);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2010-09-15 16:11:12 +02:00
|
|
|
if((r=pt_bind(&vmc->vm_pt, vmc)) != OK)
|
|
|
|
panic("fork can't pt_bind: %d", r);
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
if(fullvm) {
|
2009-09-21 16:49:49 +02:00
|
|
|
vir_bytes vir;
|
|
|
|
/* making these messages writable is an optimisation
|
|
|
|
* and its return value needn't be checked.
|
|
|
|
*/
|
|
|
|
vir = arch_vir2map(vmc, msgaddr);
|
2011-02-27 00:00:55 +01:00
|
|
|
if (handle_memory(vmc, vir, sizeof(message), 1) != OK)
|
|
|
|
panic("do_fork: handle_memory for child failed\n");
|
2009-09-21 16:49:49 +02:00
|
|
|
vir = arch_vir2map(vmp, msgaddr);
|
2011-02-27 00:00:55 +01:00
|
|
|
if (handle_memory(vmp, vir, sizeof(message), 1) != OK)
|
|
|
|
panic("do_fork: handle_memory for parent failed\n");
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Inform caller of new child endpoint. */
|
|
|
|
msg->VMF_CHILD_ENDPOINT = vmc->vm_endpoint;
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|