minix/kernel/arch/earm/memory.c
Ben Gras 565f13088f make vfs & filesystems use failable copying
Change the kernel to add features to vircopy and safecopies so that
transparent copy fixing won't happen to avoid deadlocks, and such copies
fail with EFAULT.

Transparently making copying work from filesystems (as normally done by
the kernel & VM when copying fails because of missing/readonly memory)
is problematic as it can happen that, for file-mapped ranges, that that
same filesystem that is blocked on the copy request is needed to satisfy
the memory range, leading to deadlock. Dito for VFS itself, if done with
a blocking call.

This change makes the copying done from a filesystem fail in such cases
with EFAULT by VFS adding the CPF_TRY flag to the grants. If a FS call
fails with EFAULT, VFS will then request the range to be made available
to VM after the FS is unblocked, allowing it to be used to satisfy the
range if need be in another VFS thread.

Similarly, for datacopies that VFS itself does, it uses the failable
vircopy variant and callers use a wrapper that talk to VM if necessary
to get the copy to work.

	. kernel: add CPF_TRY flag to safecopies
	. kernel: only request writable ranges to VM for the
	  target buffer when copying fails
	. do copying in VFS TRY-first
	. some fixes in VM to build SANITYCHECK mode
	. add regression test for the cases where
	  - a FS system call needs memory mapped in a process that the
	    FS itself must map.
	  - such a range covers more than one file-mapped region.
	. add 'try' mode to vircopy, physcopy
	. add flags field to copy kernel call messages
	. if CP_FLAG_TRY is set, do not transparently try
	  to fix memory ranges
	. for use by VFS when accessing user buffers to avoid
	  deadlock
	. remove some obsolete backwards compatability assignments
        . VFS: let thread scheduling work for VM requests too
          Allows VFS to make calls to VM while suspending and resuming
          the currently running thread. Does currently not work for the
          main thread.
        . VM: add fix memory range call for use by VFS

Change-Id: I295794269cea51a3163519a9cfe5901301d90b32
2014-07-28 17:05:14 +02:00

899 lines
25 KiB
C

#include "kernel/kernel.h"
#include "kernel/proc.h"
#include "kernel/vm.h"
#include <machine/vm.h>
#include <minix/type.h>
#include <minix/board.h>
#include <minix/syslib.h>
#include <minix/cpufeature.h>
#include <string.h>
#include <assert.h>
#include <signal.h>
#include <stdlib.h>
#include <machine/vm.h>
#include "arch_proto.h"
#include "kernel/proto.h"
#include "kernel/debug.h"
#include "bsp_timer.h"
#define HASPT(procptr) ((procptr)->p_seg.p_ttbr != 0)
static int nfreepdes = 0;
#define MAXFREEPDES 2
static int freepdes[MAXFREEPDES];
static u32_t phys_get32(phys_bytes v);
/* list of requested physical mapping */
static kern_phys_map *kern_phys_map_head;
void mem_clear_mapcache(void)
{
int i;
for(i = 0; i < nfreepdes; i++) {
struct proc *ptproc = get_cpulocal_var(ptproc);
int pde = freepdes[i];
u32_t *ptv;
assert(ptproc);
ptv = ptproc->p_seg.p_ttbr_v;
assert(ptv);
ptv[pde] = 0;
}
}
/* This function sets up a mapping from within the kernel's address
* space to any other area of memory, either straight physical
* memory (pr == NULL) or a process view of memory, in 1MB windows.
* I.e., it maps in 1MB chunks of virtual (or physical) address space
* to 1MB chunks of kernel virtual address space.
*
* It recognizes pr already being in memory as a special case (no
* mapping required).
*
* The target (i.e. in-kernel) mapping area is one of the freepdes[]
* VM has earlier already told the kernel about that is available. It is
* identified as the 'pde' parameter. This value can be chosen freely
* by the caller, as long as it is in range (i.e. 0 or higher and corresponds
* to a known freepde slot). It is up to the caller to keep track of which
* freepde's are in use, and to determine which ones are free to use.
*
* The logical number supplied by the caller is translated into an actual
* pde number to be used, and a pointer to it (linear address) is returned
* for actual use by phys_copy or memset.
*/
static phys_bytes createpde(
const struct proc *pr, /* Requested process, NULL for physical. */
const phys_bytes linaddr,/* Address after segment translation. */
phys_bytes *bytes, /* Size of chunk, function may truncate it. */
int free_pde_idx, /* index of the free slot to use */
int *changed /* If mapping is made, this is set to 1. */
)
{
u32_t pdeval;
phys_bytes offset;
int pde;
assert(free_pde_idx >= 0 && free_pde_idx < nfreepdes);
pde = freepdes[free_pde_idx];
assert(pde >= 0 && pde < 4096);
if(pr && ((pr == get_cpulocal_var(ptproc)) || iskernelp(pr))) {
/* Process memory is requested, and
* it's a process that is already in current page table, or
* the kernel, which is always there.
* Therefore linaddr is valid directly, with the requested
* size.
*/
return linaddr;
}
if(pr) {
/* Requested address is in a process that is not currently
* accessible directly. Grab the PDE entry of that process'
* page table that corresponds to the requested address.
*/
assert(pr->p_seg.p_ttbr_v);
pdeval = pr->p_seg.p_ttbr_v[ARM_VM_PDE(linaddr)];
} else {
/* Requested address is physical. Make up the PDE entry. */
assert (linaddr >= PHYS_MEM_BEGIN && linaddr <= PHYS_MEM_END);
/* memory */
pdeval = (linaddr & ARM_VM_SECTION_MASK)
| ARM_VM_SECTION
| ARM_VM_SECTION_DOMAIN
| ARM_VM_SECTION_CACHED
| ARM_VM_SECTION_USER;
}
/* Write the pde value that we need into a pde that the kernel
* can access, into the currently loaded page table so it becomes
* visible.
*/
assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v);
if(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v[pde] != pdeval) {
get_cpulocal_var(ptproc)->p_seg.p_ttbr_v[pde] = pdeval;
*changed = 1;
}
/* Memory is now available, but only the 1MB window of virtual
* address space that we have mapped; calculate how much of
* the requested range is visible and return that in *bytes,
* if that is less than the requested range.
*/
offset = linaddr & ARM_VM_OFFSET_MASK_1MB; /* Offset in 1MB window. */
*bytes = MIN(*bytes, ARM_SECTION_SIZE - offset);
/* Return the linear address of the start of the new mapping. */
return ARM_SECTION_SIZE*pde + offset;
}
/*===========================================================================*
* check_resumed_caller *
*===========================================================================*/
static int check_resumed_caller(struct proc *caller)
{
/* Returns the result from VM if caller was resumed, otherwise OK. */
if (caller && (caller->p_misc_flags & MF_KCALL_RESUME)) {
assert(caller->p_vmrequest.vmresult != VMSUSPEND);
return caller->p_vmrequest.vmresult;
}
return OK;
}
/*===========================================================================*
* lin_lin_copy *
*===========================================================================*/
static int lin_lin_copy(struct proc *srcproc, vir_bytes srclinaddr,
struct proc *dstproc, vir_bytes dstlinaddr, vir_bytes bytes)
{
u32_t addr;
proc_nr_t procslot;
assert(get_cpulocal_var(ptproc));
assert(get_cpulocal_var(proc_ptr));
assert(read_ttbr0() == get_cpulocal_var(ptproc)->p_seg.p_ttbr);
procslot = get_cpulocal_var(ptproc)->p_nr;
assert(procslot >= 0 && procslot < ARM_VM_DIR_ENTRIES);
if(srcproc) assert(!RTS_ISSET(srcproc, RTS_SLOT_FREE));
if(dstproc) assert(!RTS_ISSET(dstproc, RTS_SLOT_FREE));
assert(!RTS_ISSET(get_cpulocal_var(ptproc), RTS_SLOT_FREE));
assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v);
if(srcproc) assert(!RTS_ISSET(srcproc, RTS_VMINHIBIT));
if(dstproc) assert(!RTS_ISSET(dstproc, RTS_VMINHIBIT));
while(bytes > 0) {
phys_bytes srcptr, dstptr;
vir_bytes chunk = bytes;
int changed = 0;
#ifdef CONFIG_SMP
unsigned cpu = cpuid;
if (srcproc && GET_BIT(srcproc->p_stale_tlb, cpu)) {
changed = 1;
UNSET_BIT(srcproc->p_stale_tlb, cpu);
}
if (dstproc && GET_BIT(dstproc->p_stale_tlb, cpu)) {
changed = 1;
UNSET_BIT(dstproc->p_stale_tlb, cpu);
}
#endif
/* Set up 1MB ranges. */
srcptr = createpde(srcproc, srclinaddr, &chunk, 0, &changed);
dstptr = createpde(dstproc, dstlinaddr, &chunk, 1, &changed);
if(changed) {
reload_ttbr0();
}
/* Copy pages. */
PHYS_COPY_CATCH(srcptr, dstptr, chunk, addr);
if(addr) {
/* If addr is nonzero, a page fault was caught.
*
* phys_copy does all memory accesses word-aligned (rounded
* down), so pagefaults can occur at a lower address than
* the specified offsets. compute the lower bounds for sanity
* check use.
*/
vir_bytes src_aligned = srcptr & ~0x3, dst_aligned = dstptr & ~0x3;
if(addr >= src_aligned && addr < (srcptr + chunk)) {
return EFAULT_SRC;
}
if(addr >= dst_aligned && addr < (dstptr + chunk)) {
return EFAULT_DST;
}
panic("lin_lin_copy fault out of range");
/* Not reached. */
return EFAULT;
}
/* Update counter and addresses for next iteration, if any. */
bytes -= chunk;
srclinaddr += chunk;
dstlinaddr += chunk;
}
if(srcproc) assert(!RTS_ISSET(srcproc, RTS_SLOT_FREE));
if(dstproc) assert(!RTS_ISSET(dstproc, RTS_SLOT_FREE));
assert(!RTS_ISSET(get_cpulocal_var(ptproc), RTS_SLOT_FREE));
assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v);
return OK;
}
static u32_t phys_get32(phys_bytes addr)
{
u32_t v;
int r;
if((r=lin_lin_copy(NULL, addr,
proc_addr(SYSTEM), (phys_bytes) &v, sizeof(v))) != OK) {
panic("lin_lin_copy for phys_get32 failed: %d", r);
}
return v;
}
/*===========================================================================*
* umap_virtual *
*===========================================================================*/
phys_bytes umap_virtual(rp, seg, vir_addr, bytes)
register struct proc *rp; /* pointer to proc table entry for process */
int seg; /* T, D, or S segment */
vir_bytes vir_addr; /* virtual address in bytes within the seg */
vir_bytes bytes; /* # of bytes to be copied */
{
phys_bytes phys = 0;
if(vm_lookup(rp, vir_addr, &phys, NULL) != OK) {
printf("SYSTEM:umap_virtual: vm_lookup of %s: seg 0x%x: 0x%lx failed\n", rp->p_name, seg, vir_addr);
phys = 0;
} else {
if(phys == 0)
panic("vm_lookup returned phys: 0x%lx", phys);
}
if(phys == 0) {
printf("SYSTEM:umap_virtual: lookup failed\n");
return 0;
}
/* Now make sure addresses are contiguous in physical memory
* so that the umap makes sense.
*/
if(bytes > 0 && vm_lookup_range(rp, vir_addr, NULL, bytes) != bytes) {
printf("umap_virtual: %s: %lu at 0x%lx (vir 0x%lx) not contiguous\n",
rp->p_name, bytes, vir_addr, vir_addr);
return 0;
}
/* phys must be larger than 0 (or the caller will think the call
* failed), and address must not cross a page boundary.
*/
assert(phys);
return phys;
}
/*===========================================================================*
* vm_lookup *
*===========================================================================*/
int vm_lookup(const struct proc *proc, const vir_bytes virtual,
phys_bytes *physical, u32_t *ptent)
{
u32_t *root, *pt;
int pde, pte;
u32_t pde_v, pte_v;
assert(proc);
assert(physical);
assert(!isemptyp(proc));
assert(HASPT(proc));
/* Retrieve page directory entry. */
root = (u32_t *) (proc->p_seg.p_ttbr & ARM_TTBR_ADDR_MASK);
assert(!((u32_t) root % ARM_PAGEDIR_SIZE));
pde = ARM_VM_PDE(virtual);
assert(pde >= 0 && pde < ARM_VM_DIR_ENTRIES);
pde_v = phys_get32((u32_t) (root + pde));
if(! ((pde_v & ARM_VM_PDE_PRESENT)
|| (pde_v & ARM_VM_SECTION_PRESENT)
)) {
return EFAULT;
}
if(pde_v & ARM_VM_SECTION) {
*physical = pde_v & ARM_VM_SECTION_MASK;
if(ptent) *ptent = pde_v;
*physical += virtual & ARM_VM_OFFSET_MASK_1MB;
} else {
/* Retrieve page table entry. */
pt = (u32_t *) (pde_v & ARM_VM_PDE_MASK);
assert(!((u32_t) pt % ARM_PAGETABLE_SIZE));
pte = ARM_VM_PTE(virtual);
assert(pte >= 0 && pte < ARM_VM_PT_ENTRIES);
pte_v = phys_get32((u32_t) (pt + pte));
if(!(pte_v & ARM_VM_PTE_PRESENT)) {
return EFAULT;
}
if(ptent) *ptent = pte_v;
/* Actual address now known; retrieve it and add page offset. */
*physical = pte_v & ARM_VM_PTE_MASK;
*physical += virtual % ARM_PAGE_SIZE;
}
return OK;
}
/*===========================================================================*
* vm_lookup_range *
*===========================================================================*/
size_t vm_lookup_range(const struct proc *proc, vir_bytes vir_addr,
phys_bytes *phys_addr, size_t bytes)
{
/* Look up the physical address corresponding to linear virtual address
* 'vir_addr' for process 'proc'. Return the size of the range covered
* by contiguous physical memory starting from that address; this may
* be anywhere between 0 and 'bytes' inclusive. If the return value is
* nonzero, and 'phys_addr' is non-NULL, 'phys_addr' will be set to the
* base physical address of the range. 'vir_addr' and 'bytes' need not
* be page-aligned, but the caller must have verified that the given
* linear range is valid for the given process at all.
*/
phys_bytes phys, next_phys;
size_t len;
assert(proc);
assert(bytes > 0);
assert(HASPT(proc));
/* Look up the first page. */
if (vm_lookup(proc, vir_addr, &phys, NULL) != OK)
return 0;
if (phys_addr != NULL)
*phys_addr = phys;
len = ARM_PAGE_SIZE - (vir_addr % ARM_PAGE_SIZE);
vir_addr += len;
next_phys = phys + len;
/* Look up any next pages and test physical contiguity. */
while (len < bytes) {
if (vm_lookup(proc, vir_addr, &phys, NULL) != OK)
break;
if (next_phys != phys)
break;
len += ARM_PAGE_SIZE;
vir_addr += ARM_PAGE_SIZE;
next_phys += ARM_PAGE_SIZE;
}
/* We might now have overshot the requested length somewhat. */
return MIN(bytes, len);
}
/*===========================================================================*
* vm_suspend *
*===========================================================================*/
static void vm_suspend(struct proc *caller, const struct proc *target,
const vir_bytes linaddr, const vir_bytes len, const int type,
const int writeflag)
{
/* This range is not OK for this process. Set parameters
* of the request and notify VM about the pending request.
*/
assert(!RTS_ISSET(caller, RTS_VMREQUEST));
assert(!RTS_ISSET(target, RTS_VMREQUEST));
RTS_SET(caller, RTS_VMREQUEST);
caller->p_vmrequest.req_type = VMPTYPE_CHECK;
caller->p_vmrequest.target = target->p_endpoint;
caller->p_vmrequest.params.check.start = linaddr;
caller->p_vmrequest.params.check.length = len;
caller->p_vmrequest.params.check.writeflag = writeflag;
caller->p_vmrequest.type = type;
/* Connect caller on vmrequest wait queue. */
if(!(caller->p_vmrequest.nextrequestor = vmrequest))
if(OK != send_sig(VM_PROC_NR, SIGKMEM))
panic("send_sig failed");
vmrequest = caller;
}
/*===========================================================================*
* vm_check_range *
*===========================================================================*/
int vm_check_range(struct proc *caller, struct proc *target,
vir_bytes vir_addr, size_t bytes, int writeflag)
{
/* Public interface to vm_suspend(), for use by kernel calls. On behalf
* of 'caller', call into VM to check linear virtual address range of
* process 'target', starting at 'vir_addr', for 'bytes' bytes. This
* function assumes that it will called twice if VM returned an error
* the first time (since nothing has changed in that case), and will
* then return the error code resulting from the first call. Upon the
* first call, a non-success error code is returned as well.
*/
int r;
if ((caller->p_misc_flags & MF_KCALL_RESUME) &&
(r = caller->p_vmrequest.vmresult) != OK)
return r;
vm_suspend(caller, target, vir_addr, bytes, VMSTYPE_KERNELCALL,
writeflag);
return VMSUSPEND;
}
/*===========================================================================*
* delivermsg *
*===========================================================================*/
void delivermsg(struct proc *rp)
{
int r = OK;
assert(rp->p_misc_flags & MF_DELIVERMSG);
assert(rp->p_delivermsg.m_source != NONE);
if (copy_msg_to_user(&rp->p_delivermsg,
(message *) rp->p_delivermsg_vir)) {
printf("WARNING wrong user pointer 0x%08lx from "
"process %s / %d\n",
rp->p_delivermsg_vir,
rp->p_name,
rp->p_endpoint);
r = EFAULT;
}
/* Indicate message has been delivered; address is 'used'. */
rp->p_delivermsg.m_source = NONE;
rp->p_misc_flags &= ~MF_DELIVERMSG;
if(!(rp->p_misc_flags & MF_CONTEXT_SET)) {
rp->p_reg.retreg = r;
}
}
/*===========================================================================*
* vmmemset *
*===========================================================================*/
int vm_memset(struct proc* caller, endpoint_t who, phys_bytes ph, int c,
phys_bytes count)
{
u32_t pattern;
struct proc *whoptr = NULL;
phys_bytes cur_ph = ph;
phys_bytes left = count;
phys_bytes ptr, chunk, pfa = 0;
int new_ttbr, r = OK;
if ((r = check_resumed_caller(caller)) != OK)
return r;
/* NONE for physical, otherwise virtual */
if (who != NONE && !(whoptr = endpoint_lookup(who)))
return ESRCH;
c &= 0xFF;
pattern = c | (c << 8) | (c << 16) | (c << 24);
assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v);
assert(!catch_pagefaults);
catch_pagefaults = 1;
/* We can memset as many bytes as we have remaining,
* or as many as remain in the 1MB chunk we mapped in.
*/
while (left > 0) {
new_ttbr = 0;
chunk = left;
ptr = createpde(whoptr, cur_ph, &chunk, 0, &new_ttbr);
if (new_ttbr) {
reload_ttbr0();
}
/* If a page fault happens, pfa is non-null */
if ((pfa = phys_memset(ptr, pattern, chunk))) {
/* If a process pagefaults, VM may help out */
if (whoptr) {
vm_suspend(caller, whoptr, ph, count,
VMSTYPE_KERNELCALL, 1);
assert(catch_pagefaults);
catch_pagefaults = 0;
return VMSUSPEND;
}
/* Pagefault when phys copying ?! */
panic("vm_memset: pf %lx addr=%lx len=%lu\n",
pfa , ptr, chunk);
}
cur_ph += chunk;
left -= chunk;
}
assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v);
assert(catch_pagefaults);
catch_pagefaults = 0;
return OK;
}
/*===========================================================================*
* virtual_copy_f *
*===========================================================================*/
int virtual_copy_f(caller, src_addr, dst_addr, bytes, vmcheck)
struct proc * caller;
struct vir_addr *src_addr; /* source virtual address */
struct vir_addr *dst_addr; /* destination virtual address */
vir_bytes bytes; /* # of bytes to copy */
int vmcheck; /* if nonzero, can return VMSUSPEND */
{
/* Copy bytes from virtual address src_addr to virtual address dst_addr. */
struct vir_addr *vir_addr[2]; /* virtual source and destination address */
int i, r;
struct proc *procs[2];
assert((vmcheck && caller) || (!vmcheck && !caller));
/* Check copy count. */
if (bytes <= 0) return(EDOM);
/* Do some more checks and map virtual addresses to physical addresses. */
vir_addr[_SRC_] = src_addr;
vir_addr[_DST_] = dst_addr;
for (i=_SRC_; i<=_DST_; i++) {
endpoint_t proc_e = vir_addr[i]->proc_nr_e;
int proc_nr;
struct proc *p;
if(proc_e == NONE) {
p = NULL;
} else {
if(!isokendpt(proc_e, &proc_nr)) {
printf("virtual_copy: no reasonable endpoint\n");
return ESRCH;
}
p = proc_addr(proc_nr);
}
procs[i] = p;
}
if ((r = check_resumed_caller(caller)) != OK)
return r;
if((r=lin_lin_copy(procs[_SRC_], vir_addr[_SRC_]->offset,
procs[_DST_], vir_addr[_DST_]->offset, bytes)) != OK) {
int writeflag;
struct proc *target = NULL;
phys_bytes lin;
if(r != EFAULT_SRC && r != EFAULT_DST)
panic("lin_lin_copy failed: %d", r);
if(!vmcheck || !caller) {
return r;
}
if(r == EFAULT_SRC) {
lin = vir_addr[_SRC_]->offset;
target = procs[_SRC_];
writeflag = 0;
} else if(r == EFAULT_DST) {
lin = vir_addr[_DST_]->offset;
target = procs[_DST_];
writeflag = 1;
} else {
panic("r strange: %d", r);
}
assert(caller);
assert(target);
vm_suspend(caller, target, lin, bytes, VMSTYPE_KERNELCALL, writeflag);
return VMSUSPEND;
}
return OK;
}
/*===========================================================================*
* data_copy *
*===========================================================================*/
int data_copy(const endpoint_t from_proc, const vir_bytes from_addr,
const endpoint_t to_proc, const vir_bytes to_addr,
size_t bytes)
{
struct vir_addr src, dst;
src.offset = from_addr;
dst.offset = to_addr;
src.proc_nr_e = from_proc;
dst.proc_nr_e = to_proc;
assert(src.proc_nr_e != NONE);
assert(dst.proc_nr_e != NONE);
return virtual_copy(&src, &dst, bytes);
}
/*===========================================================================*
* data_copy_vmcheck *
*===========================================================================*/
int data_copy_vmcheck(struct proc * caller,
const endpoint_t from_proc, const vir_bytes from_addr,
const endpoint_t to_proc, const vir_bytes to_addr,
size_t bytes)
{
struct vir_addr src, dst;
src.offset = from_addr;
dst.offset = to_addr;
src.proc_nr_e = from_proc;
dst.proc_nr_e = to_proc;
assert(src.proc_nr_e != NONE);
assert(dst.proc_nr_e != NONE);
return virtual_copy_vmcheck(caller, &src, &dst, bytes);
}
void memory_init(void)
{
assert(nfreepdes == 0);
freepdes[nfreepdes++] = kinfo.freepde_start++;
freepdes[nfreepdes++] = kinfo.freepde_start++;
assert(kinfo.freepde_start < ARM_VM_DIR_ENTRIES);
assert(nfreepdes == 2);
assert(nfreepdes <= MAXFREEPDES);
}
/*===========================================================================*
* arch_proc_init *
*===========================================================================*/
void arch_proc_init(struct proc *pr, const u32_t ip, const u32_t sp,
const u32_t ps_str, char *name)
{
arch_proc_reset(pr);
strcpy(pr->p_name, name);
/* set custom state we know */
pr->p_reg.pc = ip;
pr->p_reg.sp = sp;
pr->p_reg.retreg = ps_str; /* a.k.a r0*/
}
static int usermapped_glo_index = -1,
usermapped_index = -1, first_um_idx = -1;
/* defined in kernel.lds */
extern char usermapped_start, usermapped_end, usermapped_nonglo_start;
int arch_phys_map(const int index,
phys_bytes *addr,
phys_bytes *len,
int *flags)
{
static int first = 1;
kern_phys_map *phys_maps;
int freeidx = 0;
u32_t glo_len = (u32_t) &usermapped_nonglo_start -
(u32_t) &usermapped_start;
if(first) {
memset(&minix_kerninfo, 0, sizeof(minix_kerninfo));
if(glo_len > 0) {
usermapped_glo_index = freeidx++;
}
usermapped_index = freeidx++;
first_um_idx = usermapped_index;
if(usermapped_glo_index != -1)
first_um_idx = usermapped_glo_index;
first = 0;
/* list over the maps and index them */
phys_maps = kern_phys_map_head;
while(phys_maps != NULL){
phys_maps->index = freeidx++;
phys_maps = phys_maps->next;
}
}
if(index == usermapped_glo_index) {
*addr = vir2phys(&usermapped_start);
*len = glo_len;
*flags = VMMF_USER | VMMF_GLO;
return OK;
}
else if(index == usermapped_index) {
*addr = vir2phys(&usermapped_nonglo_start);
*len = (u32_t) &usermapped_end -
(u32_t) &usermapped_nonglo_start;
*flags = VMMF_USER;
return OK;
}
/* if this all fails loop over the maps */
phys_maps = kern_phys_map_head;
while(phys_maps != NULL){
if(phys_maps->index == index){
*addr = phys_maps->addr;
*len = phys_maps->size;
*flags = phys_maps->vm_flags;
return OK;
}
phys_maps = phys_maps->next;
}
return EINVAL;
}
int arch_phys_map_reply(const int index, const vir_bytes addr)
{
kern_phys_map *phys_maps;
if(index == first_um_idx) {
u32_t usermapped_offset;
assert(addr > (u32_t) &usermapped_start);
usermapped_offset = addr - (u32_t) &usermapped_start;
#define FIXEDPTR(ptr) (void *) ((u32_t)ptr + usermapped_offset)
#define FIXPTR(ptr) ptr = FIXEDPTR(ptr)
#define ASSIGN(minixstruct) minix_kerninfo.minixstruct = FIXEDPTR(&minixstruct)
ASSIGN(kinfo);
ASSIGN(machine);
ASSIGN(kmessages);
ASSIGN(loadinfo);
/* adjust the pointers of the functions and the struct
* itself to the user-accessible mapping
*/
minix_kerninfo.kerninfo_magic = KERNINFO_MAGIC;
minix_kerninfo.minix_feature_flags = minix_feature_flags;
minix_kerninfo_user = (vir_bytes) FIXEDPTR(&minix_kerninfo);
return OK;
}
if (index == usermapped_index) {
return OK;
}
/* if this all fails loop over the maps */
/* list over the maps and index them */
phys_maps = kern_phys_map_head;
while(phys_maps != NULL){
if(phys_maps->index == index){
assert(phys_maps->cb != NULL);
/* only update the vir addr we are
going to call the callback in enable
paging
*/
phys_maps->vir = addr;
return OK;
}
phys_maps = phys_maps->next;
}
return EINVAL;
}
int arch_enable_paging(struct proc * caller)
{
kern_phys_map *phys_maps;
assert(caller->p_seg.p_ttbr);
/* load caller's page table */
switch_address_space(caller);
/* We have now switched address spaces and the mappings are
valid. We can now remap previous mappings. This is not a
good time to do printf as the initial massing is gone and
the new mapping is not in place */
phys_maps = kern_phys_map_head;
while(phys_maps != NULL){
assert(phys_maps->cb != NULL);
phys_maps->cb(phys_maps->id, phys_maps->vir);
phys_maps = phys_maps->next;
}
return OK;
}
void release_address_space(struct proc *pr)
{
pr->p_seg.p_ttbr_v = NULL;
barrier();
}
/*
* Request a physical mapping
*/
int kern_req_phys_map( phys_bytes base_address, vir_bytes io_size,
int vm_flags, kern_phys_map * priv,
kern_phys_map_mapped cb, vir_bytes id)
{
/* Assign the values to the given struct and add priv
to the list */
assert(base_address != 0);
assert(io_size % ARM_PAGE_SIZE == 0);
assert(cb != NULL);
priv->addr = base_address;
priv->size = io_size;
priv->vm_flags = vm_flags;
priv->cb = cb;
priv->id = id;
priv->index = -1;
priv->next = NULL;
if (kern_phys_map_head == NULL){
/* keep a list of items this is the first one */
kern_phys_map_head = priv;
kern_phys_map_head->next = NULL;
} else {
/* insert the item head but first keep track
of the current by putting it in next */
priv->next = kern_phys_map_head;
/* replace the head */
kern_phys_map_head = priv;
}
return 0;
}
/*
* Callback implementation where the id given to the
* kern_phys_map is a pointer to the io map base address.
* this implementation will just change that base address.
* once that area is remapped.
*/
int kern_phys_map_mapped_ptr(vir_bytes id, phys_bytes address){
*((vir_bytes*)id) = address;
return 0;
}
/*
* Request a physical mapping and put the result in the given prt
* Note that ptr will only be valid once the callback happened.
*/
int kern_phys_map_ptr(
phys_bytes base_address,
vir_bytes io_size,
int vm_flags,
kern_phys_map * priv,
vir_bytes ptr)
{
return kern_req_phys_map(base_address,io_size,vm_flags,priv,kern_phys_map_mapped_ptr,ptr);
}