2005-10-14 10:58:59 +02:00
|
|
|
/* The kernel call implemented in this file:
|
2005-07-14 17:12:12 +02:00
|
|
|
* m_type: SYS_UMAP
|
|
|
|
*
|
2005-10-14 10:58:59 +02:00
|
|
|
* The parameters for this kernel call are:
|
2005-07-14 17:12:12 +02:00
|
|
|
* m5_i1: CP_SRC_PROC_NR (process number)
|
2010-01-19 22:19:59 +01:00
|
|
|
* m5_s1: CP_SRC_SPACE (segment where address is: T, D, or S)
|
2005-07-14 17:12:12 +02:00
|
|
|
* m5_l1: CP_SRC_ADDR (virtual address)
|
|
|
|
* m5_l2: CP_DST_ADDR (returns physical address)
|
|
|
|
* m5_l3: CP_NR_BYTES (size of datastructure)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../system.h"
|
|
|
|
|
2010-02-03 10:04:48 +01:00
|
|
|
#include <minix/endpoint.h>
|
|
|
|
|
2005-07-14 17:12:12 +02:00
|
|
|
#if USE_UMAP
|
|
|
|
|
|
|
|
/*==========================================================================*
|
|
|
|
* do_umap *
|
|
|
|
*==========================================================================*/
|
2010-02-03 10:04:48 +01:00
|
|
|
PUBLIC int do_umap(struct proc * caller, message * m_ptr)
|
2005-07-14 17:12:12 +02:00
|
|
|
{
|
|
|
|
/* Map virtual address to physical, for non-kernel processes. */
|
|
|
|
int seg_type = m_ptr->CP_SRC_SPACE & SEGMENT_TYPE;
|
|
|
|
int seg_index = m_ptr->CP_SRC_SPACE & SEGMENT_INDEX;
|
|
|
|
vir_bytes offset = m_ptr->CP_SRC_ADDR;
|
|
|
|
int count = m_ptr->CP_NR_BYTES;
|
'proc number' is process slot, 'endpoint' are generation-aware process
instance numbers, encoded and decoded using macros in <minix/endpoint.h>.
proc number -> endpoint migration
. proc_nr in the interrupt hook is now an endpoint, proc_nr_e.
. m_source for messages and notifies is now an endpoint, instead of
proc number.
. isokendpt() converts an endpoint to a process number, returns
success (but fails if the process number is out of range, the
process slot is not a living process, or the given endpoint
number does not match the endpoint number in the process slot,
indicating an old process).
. okendpt() is the same as isokendpt(), but panic()s if the conversion
fails. This is mainly used for decoding message.m_source endpoints,
and other endpoint numbers in kernel data structures, which should
always be correct.
. if DEBUG_ENABLE_IPC_WARNINGS is enabled, isokendpt() and okendpt()
get passed the __FILE__ and __LINE__ of the calling lines, and
print messages about what is wrong with the endpoint number
(out of range proc, empty proc, or inconsistent endpoint number),
with the caller, making finding where the conversion failed easy
without having to include code for every call to print where things
went wrong. Sometimes this is harmless (wrong arg to a kernel call),
sometimes it's a fatal internal inconsistency (bogus m_source).
. some process table fields have been appended an _e to indicate it's
become and endpoint.
. process endpoint is stored in p_endpoint, without generation number.
it turns out the kernel never needs the generation number, except
when fork()ing, so it's decoded then.
. kernel calls all take endpoints as arguments, not proc numbers.
the one exception is sys_fork(), which needs to know in which slot
to put the child.
2006-03-03 11:00:02 +01:00
|
|
|
int endpt = (int) m_ptr->CP_SRC_ENDPT;
|
2008-11-19 13:26:10 +01:00
|
|
|
int proc_nr, r;
|
|
|
|
int naughty = 0;
|
|
|
|
phys_bytes phys_addr = 0, lin_addr = 0;
|
2010-02-03 10:04:48 +01:00
|
|
|
struct proc *targetpr;
|
2005-07-14 17:12:12 +02:00
|
|
|
|
|
|
|
/* Verify process number. */
|
'proc number' is process slot, 'endpoint' are generation-aware process
instance numbers, encoded and decoded using macros in <minix/endpoint.h>.
proc number -> endpoint migration
. proc_nr in the interrupt hook is now an endpoint, proc_nr_e.
. m_source for messages and notifies is now an endpoint, instead of
proc number.
. isokendpt() converts an endpoint to a process number, returns
success (but fails if the process number is out of range, the
process slot is not a living process, or the given endpoint
number does not match the endpoint number in the process slot,
indicating an old process).
. okendpt() is the same as isokendpt(), but panic()s if the conversion
fails. This is mainly used for decoding message.m_source endpoints,
and other endpoint numbers in kernel data structures, which should
always be correct.
. if DEBUG_ENABLE_IPC_WARNINGS is enabled, isokendpt() and okendpt()
get passed the __FILE__ and __LINE__ of the calling lines, and
print messages about what is wrong with the endpoint number
(out of range proc, empty proc, or inconsistent endpoint number),
with the caller, making finding where the conversion failed easy
without having to include code for every call to print where things
went wrong. Sometimes this is harmless (wrong arg to a kernel call),
sometimes it's a fatal internal inconsistency (bogus m_source).
. some process table fields have been appended an _e to indicate it's
become and endpoint.
. process endpoint is stored in p_endpoint, without generation number.
it turns out the kernel never needs the generation number, except
when fork()ing, so it's decoded then.
. kernel calls all take endpoints as arguments, not proc numbers.
the one exception is sys_fork(), which needs to know in which slot
to put the child.
2006-03-03 11:00:02 +01:00
|
|
|
if (endpt == SELF)
|
2010-02-03 10:04:48 +01:00
|
|
|
proc_nr = _ENDPOINT_P(caller->p_endpoint);
|
'proc number' is process slot, 'endpoint' are generation-aware process
instance numbers, encoded and decoded using macros in <minix/endpoint.h>.
proc number -> endpoint migration
. proc_nr in the interrupt hook is now an endpoint, proc_nr_e.
. m_source for messages and notifies is now an endpoint, instead of
proc number.
. isokendpt() converts an endpoint to a process number, returns
success (but fails if the process number is out of range, the
process slot is not a living process, or the given endpoint
number does not match the endpoint number in the process slot,
indicating an old process).
. okendpt() is the same as isokendpt(), but panic()s if the conversion
fails. This is mainly used for decoding message.m_source endpoints,
and other endpoint numbers in kernel data structures, which should
always be correct.
. if DEBUG_ENABLE_IPC_WARNINGS is enabled, isokendpt() and okendpt()
get passed the __FILE__ and __LINE__ of the calling lines, and
print messages about what is wrong with the endpoint number
(out of range proc, empty proc, or inconsistent endpoint number),
with the caller, making finding where the conversion failed easy
without having to include code for every call to print where things
went wrong. Sometimes this is harmless (wrong arg to a kernel call),
sometimes it's a fatal internal inconsistency (bogus m_source).
. some process table fields have been appended an _e to indicate it's
become and endpoint.
. process endpoint is stored in p_endpoint, without generation number.
it turns out the kernel never needs the generation number, except
when fork()ing, so it's decoded then.
. kernel calls all take endpoints as arguments, not proc numbers.
the one exception is sys_fork(), which needs to know in which slot
to put the child.
2006-03-03 11:00:02 +01:00
|
|
|
else
|
|
|
|
if (! isokendpt(endpt, &proc_nr))
|
|
|
|
return(EINVAL);
|
2008-11-19 13:26:10 +01:00
|
|
|
targetpr = proc_addr(proc_nr);
|
|
|
|
|
2005-07-14 17:12:12 +02:00
|
|
|
/* See which mapping should be made. */
|
|
|
|
switch(seg_type) {
|
|
|
|
case LOCAL_SEG:
|
2008-11-19 13:26:10 +01:00
|
|
|
phys_addr = lin_addr = umap_local(targetpr, seg_index, offset, count);
|
|
|
|
if(!lin_addr) return EFAULT;
|
|
|
|
naughty = 1;
|
2005-07-14 17:12:12 +02:00
|
|
|
break;
|
|
|
|
case REMOTE_SEG:
|
2008-11-19 13:26:10 +01:00
|
|
|
phys_addr = lin_addr = umap_remote(targetpr, seg_index, offset, count);
|
|
|
|
if(!lin_addr) return EFAULT;
|
|
|
|
naughty = 1;
|
2005-07-14 17:12:12 +02:00
|
|
|
break;
|
2008-11-19 13:26:10 +01:00
|
|
|
case LOCAL_VM_SEG:
|
Primary goal for these changes is:
- no longer have kernel have its own page table that is loaded
on every kernel entry (trap, interrupt, exception). the primary
purpose is to reduce the number of required reloads.
Result:
- kernel can only access memory of process that was running when
kernel was entered
- kernel must be mapped into every process page table, so traps to
kernel keep working
Problem:
- kernel must often access memory of arbitrary processes (e.g. send
arbitrary processes messages); this can't happen directly any more;
usually because that process' page table isn't loaded at all, sometimes
because that memory isn't mapped in at all, sometimes because it isn't
mapped in read-write.
So:
- kernel must be able to map in memory of any process, in its own
address space.
Implementation:
- VM and kernel share a range of memory in which addresses of
all page tables of all processes are available. This has two purposes:
. Kernel has to know what data to copy in order to map in a range
. Kernel has to know where to write the data in order to map it in
That last point is because kernel has to write in the currently loaded
page table.
- Processes and kernel are separated through segments; kernel segments
haven't changed.
- The kernel keeps the process whose page table is currently loaded
in 'ptproc.'
- If it wants to map in a range of memory, it writes the value of the
page directory entry for that range into the page directory entry
in the currently loaded map. There is a slot reserved for such
purposes. The kernel can then access this memory directly.
- In order to do this, its segment has been increased (and the
segments of processes start where it ends).
- In the pagefault handler, detect if the kernel is doing
'trappable' memory access (i.e. a pagefault isn't a fatal
error) and if so,
- set the saved instruction pointer to phys_copy_fault,
breaking out of phys_copy
- set the saved eax register to the address of the page
fault, both for sanity checking and for checking in
which of the two ranges that phys_copy was called
with the fault occured
- Some boot-time processes do not have their own page table,
and are mapped in with the kernel, and separated with
segments. The kernel detects this using HASPT. If such a
process has to be scheduled, any page table will work and
no page table switch is done.
Major changes in kernel are
- When accessing user processes memory, kernel no longer
explicitly checks before it does so if that memory is OK.
It simply makes the mapping (if necessary), tries to do the
operation, and traps the pagefault if that memory isn't present;
if that happens, the copy function returns EFAULT.
So all of the CHECKRANGE_OR_SUSPEND macros are gone.
- Kernel no longer has to copy/read and parse page tables.
- A message copying optimisation: when messages are copied, and
the recipient isn't mapped in, they are copied into a buffer
in the kernel. This is done in QueueMess. The next time
the recipient is scheduled, this message is copied into
its memory. This happens in schedcheck().
This eliminates the mapping/copying step for messages, and makes
it easier to deliver messages. This eliminates soft_notify.
- Kernel no longer creates a page table at all, so the vm_setbuf
and pagetable writing in memory.c is gone.
Minor changes in kernel are
- ipc_stats thrown out, wasn't used
- misc flags all renamed to MF_*
- NOREC_* macros to enter and leave functions that should not
be called recursively; just sanity checks really
- code to fully decode segment selectors and descriptors
to print on exceptions
- lots of vmassert()s added, only executed if DEBUG_VMASSERT is 1
2009-09-21 16:31:52 +02:00
|
|
|
if(seg_index == MEM_GRANT) {
|
2008-11-19 13:26:10 +01:00
|
|
|
vir_bytes newoffset;
|
|
|
|
endpoint_t newep;
|
|
|
|
int new_proc_nr;
|
|
|
|
|
|
|
|
if(verify_grant(targetpr->p_endpoint, ANY, offset, count, 0, 0,
|
|
|
|
&newoffset, &newep) != OK) {
|
|
|
|
kprintf("SYSTEM: do_umap: verify_grant in %s, grant %d, bytes 0x%lx, failed, caller %s\n", targetpr->p_name, offset, count, caller->p_name);
|
|
|
|
proc_stacktrace(caller);
|
|
|
|
return EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!isokendpt(newep, &new_proc_nr)) {
|
|
|
|
kprintf("SYSTEM: do_umap: isokendpt failed\n");
|
|
|
|
return EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* New lookup. */
|
|
|
|
offset = newoffset;
|
|
|
|
targetpr = proc_addr(new_proc_nr);
|
|
|
|
seg_index = D;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(seg_index == T || seg_index == D || seg_index == S) {
|
|
|
|
phys_addr = lin_addr = umap_local(targetpr, seg_index, offset, count);
|
|
|
|
} else {
|
|
|
|
kprintf("SYSTEM: bogus seg type 0x%lx\n", seg_index);
|
|
|
|
return EFAULT;
|
|
|
|
}
|
|
|
|
if(!lin_addr) {
|
|
|
|
kprintf("SYSTEM:do_umap: umap_local failed\n");
|
|
|
|
return EFAULT;
|
|
|
|
}
|
|
|
|
if(vm_lookup(targetpr, lin_addr, &phys_addr, NULL) != OK) {
|
|
|
|
kprintf("SYSTEM:do_umap: vm_lookup failed\n");
|
|
|
|
return EFAULT;
|
|
|
|
}
|
|
|
|
if(phys_addr == 0)
|
|
|
|
minix_panic("vm_lookup returned zero physical address", NO_NUM);
|
2006-06-20 12:03:10 +02:00
|
|
|
break;
|
2005-07-14 17:12:12 +02:00
|
|
|
default:
|
2008-11-19 13:26:10 +01:00
|
|
|
if((r=arch_umap(targetpr, offset, count, seg_type, &lin_addr))
|
|
|
|
!= OK)
|
|
|
|
return r;
|
|
|
|
phys_addr = lin_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(vm_running && !vm_contiguous(targetpr, lin_addr, count)) {
|
|
|
|
kprintf("SYSTEM:do_umap: not contiguous\n");
|
|
|
|
return EFAULT;
|
2005-07-14 17:12:12 +02:00
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2005-07-14 17:12:12 +02:00
|
|
|
m_ptr->CP_DST_ADDR = phys_addr;
|
2008-11-19 13:26:10 +01:00
|
|
|
if(naughty || phys_addr == 0) {
|
|
|
|
kprintf("kernel: umap 0x%x done by %d / %s, pc 0x%lx, 0x%lx -> 0x%lx\n",
|
2010-02-03 10:04:48 +01:00
|
|
|
seg_type, caller->p_endpoint, caller->p_name,
|
|
|
|
caller->p_reg.pc, offset, phys_addr);
|
2008-11-19 13:26:10 +01:00
|
|
|
kprintf("caller stack: ");
|
|
|
|
proc_stacktrace(caller);
|
|
|
|
}
|
2005-07-14 17:12:12 +02:00
|
|
|
return (phys_addr == 0) ? EFAULT: OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* USE_UMAP */
|