akaros/kern/include/mm.h
<<
>>
Prefs
   1/* Copyright (c) 2009, 2010 The Regents of the University of California
   2 * Barret Rhoden <brho@cs.berkeley.edu>
   3 * See LICENSE for details.
   4 *
   5 * Memory management for processes: syscall related functions, virtual memory
   6 * regions, etc. */
   7
   8#pragma once
   9
  10#include <ros/common.h>
  11#include <ros/mman.h>
  12#include <atomic.h>
  13#include <sys/queue.h>
  14#include <slab.h>
  15#include <kref.h>
  16#include <rcu.h>
  17
  18struct chan;
  19struct fd_table;
  20struct proc;    /* preprocessor games */
  21
  22#define F_OR_C_CHAN 2
  23
  24struct file_or_chan {
  25        int type;
  26        struct chan *chan;
  27        struct fs_file *fsf;    /* weak ref, set during mmap. */
  28        struct kref kref;
  29        struct rcu_head rcu;
  30};
  31
  32char *foc_to_name(struct file_or_chan *foc);
  33char *foc_abs_path(struct file_or_chan *foc);
  34ssize_t foc_read(struct file_or_chan *foc, void *buf, size_t amt, off64_t off);
  35struct file_or_chan *foc_open(char *path, int omode, int perm);
  36struct file_or_chan *fd_to_foc(struct fd_table *fdt, int fd);
  37void foc_incref(struct file_or_chan *foc);
  38void foc_decref(struct file_or_chan *foc);
  39void *foc_pointer(struct file_or_chan *foc);
  40size_t foc_get_len(struct file_or_chan *foc);
  41
  42/* Basic structure defining a region of a process's virtual memory.  Note we
  43 * don't refcnt these.  Either they are in the TAILQ/tree, or they should be
  44 * freed.  There should be no other references floating around.  We still need
  45 * to sort out how we share memory and how we'll do private memory with these
  46 * VMRs. */
  47struct vm_region {
  48        TAILQ_ENTRY(vm_region)          vm_link;
  49        TAILQ_ENTRY(vm_region)          vm_pm_link;
  50        struct proc                     *vm_proc;
  51        uintptr_t                       vm_base;
  52        uintptr_t                       vm_end;
  53        int                             vm_prot;
  54        int                             vm_flags;
  55        struct file_or_chan             *__vm_foc;
  56        size_t                          vm_foff;
  57        bool                            vm_ready; /* racy, for the PM checks */
  58        bool                            vm_shootdown_needed;
  59};
  60TAILQ_HEAD(vmr_tailq, vm_region);       /* Declares 'struct vmr_tailq' */
  61
  62static inline bool vmr_has_file(struct vm_region *vmr)
  63{
  64        return vmr->__vm_foc ? true : false;
  65}
  66
  67static inline char *vmr_to_filename(struct vm_region *vmr)
  68{
  69        assert(vmr_has_file(vmr));
  70        return foc_to_name(vmr->__vm_foc);
  71}
  72
  73void vmr_init(void);
  74void unmap_and_destroy_vmrs(struct proc *p);
  75int duplicate_vmrs(struct proc *p, struct proc *new_p);
  76void print_vmrs(struct proc *p);
  77void enumerate_vmrs(struct proc *p, void (*func)(struct vm_region *vmr, void
  78                                                 *opaque), void *opaque);
  79
  80/* mmap() related functions.  These manipulate VMRs and change the hardware page
  81 * tables.  Any requests below the LOWEST_VA will silently be upped.  This may
  82 * be a dynamic proc-specific variable later. */
  83#define MMAP_LOWEST_VA PAGE_SIZE
  84#define MMAP_LD_FIXED_VA 0x100000
  85void *mmap(struct proc *p, uintptr_t addr, size_t len, int prot, int flags,
  86           int fd, size_t offset);
  87void *do_mmap(struct proc *p, uintptr_t addr, size_t len, int prot, int flags,
  88              struct file_or_chan *foc, size_t offset);
  89int mprotect(struct proc *p, uintptr_t addr, size_t len, int prot);
  90int munmap(struct proc *p, uintptr_t addr, size_t len);
  91int handle_page_fault(struct proc *p, uintptr_t va, int prot);
  92int handle_page_fault_nofile(struct proc *p, uintptr_t va, int prot);
  93unsigned long populate_va(struct proc *p, uintptr_t va, unsigned long nr_pgs);
  94
  95/* These assume the mm_lock is held already */
  96int __do_mprotect(struct proc *p, uintptr_t addr, size_t len, int prot);
  97int __do_munmap(struct proc *p, uintptr_t addr, size_t len);
  98
  99/* Kernel Dynamic Memory Mappings */
 100struct arena *vmap_arena;
 101void vmap_init(void);
 102/* Gets PML1 page-aligned virtual addresses for the kernel's dynamic vmap.
 103 * You'll need to map it to something.  When you're done, 'put-' will also unmap
 104 * the vaddr for you. */
 105uintptr_t get_vmap_segment(size_t nr_bytes);
 106void put_vmap_segment(uintptr_t vaddr, size_t nr_bytes);
 107int map_vmap_segment(uintptr_t vaddr, uintptr_t paddr, unsigned long num_pages,
 108                     int perm);
 109/* Helper wrappers for getting and mapping a specific paddr. */
 110uintptr_t vmap_pmem(uintptr_t paddr, size_t nr_bytes);
 111uintptr_t vmap_pmem_nocache(uintptr_t paddr, size_t nr_bytes);
 112uintptr_t vmap_pmem_writecomb(uintptr_t paddr, size_t nr_bytes);
 113int vunmap_vmem(uintptr_t vaddr, size_t nr_bytes);
 114