minix/servers/vm/utility.c

358 lines
9.8 KiB
C
Raw Normal View History

/* This file contains some utility routines for VM. */
#define _SYSTEM 1
#define brk _brk /* get rid of no previous prototype warning */
#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/minlib.h>
#include <minix/type.h>
#include <minix/ipc.h>
#include <minix/sysutil.h>
#include <minix/syslib.h>
#include <minix/type.h>
#include <minix/bitmap.h>
#include <string.h>
#include <errno.h>
#include <env.h>
#include <unistd.h>
#include <assert.h>
#include <sys/param.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include "proto.h"
#include "glo.h"
#include "util.h"
#include "region.h"
#include "sanitycheck.h"
#include <machine/archtypes.h>
2010-04-02 00:22:33 +02:00
#include "kernel/const.h"
#include "kernel/config.h"
#include "kernel/type.h"
#include "kernel/proc.h"
/*===========================================================================*
* get_mem_chunks *
*===========================================================================*/
void get_mem_chunks(
struct memory *mem_chunks) /* store mem chunks here */
{
/* Initialize the free memory list from the kernel-provided memory map. Translate
* the byte offsets and sizes in this list to clicks, properly truncated.
*/
phys_bytes base, size, limit;
int i;
struct memory *memp;
/* Initialize everything to zero. */
memset(mem_chunks, 0, NR_MEMS*sizeof(*mem_chunks));
/* Obtain and parse memory from kernel environment. */
/* XXX Any memory chunk in excess of NR_MEMS is silently ignored. */
for(i = 0; i < MIN(MAXMEMMAP, NR_MEMS); i++) {
mem_chunks[i].base = kernel_boot_info.memmap[i].addr;
mem_chunks[i].size = kernel_boot_info.memmap[i].len;
}
/* Round physical memory to clicks. Round start up, round end down. */
for (i = 0; i < NR_MEMS; i++) {
memp = &mem_chunks[i]; /* next mem chunk is stored here */
base = mem_chunks[i].base;
size = mem_chunks[i].size;
limit = base + size;
base = (phys_bytes) (CLICK_CEIL(base));
limit = (phys_bytes) (CLICK_FLOOR(limit));
if (limit <= base) {
memp->base = memp->size = 0;
} else {
memp->base = base >> CLICK_SHIFT;
memp->size = (limit - base) >> CLICK_SHIFT;
}
}
}
/*===========================================================================*
* vm_isokendpt *
*===========================================================================*/
int vm_isokendpt(endpoint_t endpoint, int *procn)
{
*procn = _ENDPOINT_P(endpoint);
if(*procn < 0 || *procn >= NR_PROCS)
2010-01-19 22:00:20 +01:00
return EINVAL;
if(*procn >= 0 && endpoint != vmproc[*procn].vm_endpoint)
2010-06-24 09:37:26 +02:00
return EDEADEPT;
if(*procn >= 0 && !(vmproc[*procn].vm_flags & VMF_INUSE))
2010-06-24 09:37:26 +02:00
return EDEADEPT;
return OK;
}
/*===========================================================================*
2010-01-19 22:00:20 +01:00
* do_info *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
int do_info(message *m)
{
2010-01-19 22:00:20 +01:00
struct vm_stats_info vsi;
struct vm_usage_info vui;
static struct vm_region_info vri[MAX_VRI_COUNT];
struct vmproc *vmp;
2010-01-19 22:00:20 +01:00
vir_bytes addr, size, next, ptr;
2010-09-14 23:22:56 +02:00
int r, pr, dummy, count, free_pages, largest_contig;
2010-01-19 22:00:20 +01:00
if (vm_isokendpt(m->m_source, &pr) != OK)
return EINVAL;
vmp = &vmproc[pr];
ptr = (vir_bytes) m->VMI_PTR;
switch(m->VMI_WHAT) {
case VMIW_STATS:
vsi.vsi_pagesize = VM_PAGE_SIZE;
vsi.vsi_total = total_pages;
2010-09-14 23:22:56 +02:00
memstats(&dummy, &free_pages, &largest_contig);
vsi.vsi_free = free_pages;
vsi.vsi_largest = largest_contig;
get_stats_info(&vsi);
2010-01-19 22:00:20 +01:00
addr = (vir_bytes) &vsi;
size = sizeof(vsi);
break;
case VMIW_USAGE:
if(m->VMI_EP < 0)
get_usage_info_kernel(&vui);
else if (vm_isokendpt(m->VMI_EP, &pr) != OK)
2010-01-19 22:00:20 +01:00
return EINVAL;
else get_usage_info(&vmproc[pr], &vui);
2010-01-19 22:00:20 +01:00
addr = (vir_bytes) &vui;
size = sizeof(vui);
break;
case VMIW_REGION:
if (vm_isokendpt(m->VMI_EP, &pr) != OK)
return EINVAL;
2010-01-19 22:00:20 +01:00
count = MIN(m->VMI_COUNT, MAX_VRI_COUNT);
next = m->VMI_NEXT;
count = get_region_info(&vmproc[pr], vri, count, &next);
m->VMI_COUNT = count;
m->VMI_NEXT = next;
addr = (vir_bytes) vri;
size = sizeof(vri[0]) * count;
break;
default:
return EINVAL;
}
2010-01-19 22:00:20 +01:00
if (size == 0)
return OK;
/* Make sure that no page faults can occur while copying out. A page
* fault would cause the kernel to send a notify to us, while we would
* be waiting for the result of the copy system call, resulting in a
* deadlock. Note that no memory mapping can be undone without the
* involvement of VM, so we are safe until we're done.
*/
r = handle_memory(vmp, ptr, size, 1 /*wrflag*/, NULL, NULL, 0);
2010-01-19 22:00:20 +01:00
if (r != OK) return r;
/* Now that we know the copy out will succeed, perform the actual copy
* operation.
*/
return sys_datacopy(SELF, addr,
(vir_bytes) vmp->vm_endpoint, ptr, size);
}
/*===========================================================================*
* swap_proc_slot *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
int swap_proc_slot(struct vmproc *src_vmp, struct vmproc *dst_vmp)
{
struct vmproc orig_src_vmproc, orig_dst_vmproc;
#if LU_DEBUG
printf("VM: swap_proc: swapping %d (%d) and %d (%d)\n",
src_vmp->vm_endpoint, src_vmp->vm_slot,
dst_vmp->vm_endpoint, dst_vmp->vm_slot);
#endif
/* Save existing data. */
orig_src_vmproc = *src_vmp;
orig_dst_vmproc = *dst_vmp;
/* Swap slots. */
*src_vmp = orig_dst_vmproc;
*dst_vmp = orig_src_vmproc;
/* Preserve endpoints and slot numbers. */
src_vmp->vm_endpoint = orig_src_vmproc.vm_endpoint;
src_vmp->vm_slot = orig_src_vmproc.vm_slot;
dst_vmp->vm_endpoint = orig_dst_vmproc.vm_endpoint;
dst_vmp->vm_slot = orig_dst_vmproc.vm_slot;
#if LU_DEBUG
printf("VM: swap_proc: swapped %d (%d) and %d (%d)\n",
src_vmp->vm_endpoint, src_vmp->vm_slot,
dst_vmp->vm_endpoint, dst_vmp->vm_slot);
#endif
return OK;
}
/*===========================================================================*
* swap_proc_dyn_data *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
int swap_proc_dyn_data(struct vmproc *src_vmp, struct vmproc *dst_vmp)
{
int is_vm;
int r;
is_vm = (dst_vmp->vm_endpoint == VM_PROC_NR);
/* For VM, transfer memory regions above the stack first. */
if(is_vm) {
#if LU_DEBUG
printf("VM: swap_proc_dyn_data: tranferring regions above the stack from old VM (%d) to new VM (%d)\n",
src_vmp->vm_endpoint, dst_vmp->vm_endpoint);
#endif
No more intel/minix segments. 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.
2012-05-07 16:03:35 +02:00
r = pt_map_in_range(src_vmp, dst_vmp, VM_STACKTOP, 0);
if(r != OK) {
printf("swap_proc_dyn_data: pt_map_in_range failed\n");
return r;
}
}
#if LU_DEBUG
printf("VM: swap_proc_dyn_data: swapping regions' parents for %d (%d) and %d (%d)\n",
src_vmp->vm_endpoint, src_vmp->vm_slot,
dst_vmp->vm_endpoint, dst_vmp->vm_slot);
#endif
/* Swap vir_regions' parents. */
map_setparent(src_vmp);
map_setparent(dst_vmp);
/* For regular processes, transfer regions above the stack now.
* In case of rollback, we need to skip this step. To sandbox the
* new instance and prevent state corruption on rollback, we share all
* the regions between the two instances as COW.
*/
No more intel/minix segments. 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.
2012-05-07 16:03:35 +02:00
if(!is_vm) {
struct vir_region *vr;
vr = map_lookup(dst_vmp, VM_STACKTOP, NULL);
if(vr && !map_lookup(src_vmp, VM_STACKTOP, NULL)) {
#if LU_DEBUG
printf("VM: swap_proc_dyn_data: tranferring regions above the stack from %d to %d\n",
src_vmp->vm_endpoint, dst_vmp->vm_endpoint);
#endif
r = map_proc_copy_from(src_vmp, dst_vmp, vr);
if(r != OK) {
return r;
}
}
}
return OK;
}
void *mmap(void *addr, size_t len, int f, int f2, int f3, off_t o)
{
void *ret;
phys_bytes p;
assert(!addr);
assert(!(len % VM_PAGE_SIZE));
ret = vm_allocpages(&p, VMP_SLAB, len/VM_PAGE_SIZE);
if(!ret) return MAP_FAILED;
memset(ret, 0, len);
return ret;
}
int munmap(void * addr, size_t len)
{
vm_freepages((vir_bytes) addr, roundup(len, VM_PAGE_SIZE)/VM_PAGE_SIZE);
return 0;
}
int brk(void *addr)
{
/* brk is a special case function to allow vm itself to
allocate memory in it's own (cacheable) HEAP */
vir_bytes target = roundup((vir_bytes)addr, VM_PAGE_SIZE), v;
extern char _end;
extern char *_brksize;
static vir_bytes prevbrk = (vir_bytes) &_end;
struct vmproc *vmprocess = &vmproc[VM_PROC_NR];
for(v = roundup(prevbrk, VM_PAGE_SIZE); v < target;
v += VM_PAGE_SIZE) {
phys_bytes mem, newpage = alloc_mem(1, 0);
if(newpage == NO_MEM) return -1;
mem = CLICK2ABS(newpage);
if(pt_writemap(vmprocess, &vmprocess->vm_pt,
v, mem, VM_PAGE_SIZE,
ARCH_VM_PTE_PRESENT
| ARCH_VM_PTE_USER
| ARCH_VM_PTE_RW
#if defined(__arm__)
| ARM_VM_PTE_CACHED
#endif
, 0) != OK) {
free_mem(newpage, 1);
return -1;
}
prevbrk = v + VM_PAGE_SIZE;
}
_brksize = (char *) addr;
if(sys_vmctl(SELF, VMCTL_FLUSHTLB, 0) != OK)
panic("flushtlb failed");
return 0;
}
/*===========================================================================*
* do_getrusage *
*===========================================================================*/
int do_getrusage(message *m)
{
int res, slot;
struct vmproc *vmp;
struct rusage r_usage;
if ((res = vm_isokendpt(m->m_source, &slot)) != OK)
return ESRCH;
vmp = &vmproc[slot];
if ((res = sys_datacopy(m->m_source, (vir_bytes) m->RU_RUSAGE_ADDR,
SELF, (vir_bytes) &r_usage, (vir_bytes) sizeof(r_usage))) < 0)
return res;
r_usage.ru_maxrss = vmp->vm_total_max;
r_usage.ru_minflt = vmp->vm_minor_page_fault;
r_usage.ru_majflt = vmp->vm_major_page_fault;
return sys_datacopy(SELF, (vir_bytes) &r_usage, m->m_source,
(vir_bytes) m->RU_RUSAGE_ADDR, (vir_bytes) sizeof(r_usage));
}