• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University (SJTU)
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 #ifndef MM_VMSPACE_H
13 #define MM_VMSPACE_H
14 
15 #include <common/list.h>
16 #include <common/lock.h>
17 #include <common/rbtree.h>
18 #include <machine.h>
19 #include <arch/mmu.h>
20 #include <object/memory.h>
21 
22 /* This struct represents one virtual memory region inside on address space */
23 struct vmregion {
24     struct list_head list_node; /* As one node of the vmr_list */
25     struct rb_node tree_node; /* As one node of the vmr_tree */
26 
27     vaddr_t start;
28     size_t size;
29     vmr_prop_t perm;
30     struct pmobject *pmo;
31     struct list_head cow_private_pages;
32 };
33 
34 /* This struct represents one virtual address space */
35 struct vmspace {
36     /* List head of vmregion (vmr_list) */
37     struct list_head vmr_list;
38     /* rbtree root node of vmregion (vmr_tree) */
39     struct rb_root vmr_tree;
40 
41     /* Root page table */
42     void *pgtbl;
43     /* Address space ID for avoiding TLB conflicts */
44     unsigned long pcid;
45 
46     /* The lock for manipulating vmregions */
47     struct lock vmspace_lock;
48     /* The lock for manipulating the page table */
49     struct lock pgtbl_lock;
50 
51     /*
52      * For TLB flushing:
53      * Record the all the CPU that a vmspace ran on.
54      */
55     unsigned char history_cpus[PLAT_CPU_NUM];
56 
57     struct vmregion *heap_vmr;
58 
59     /* Records size of memory mapped. Protected by pgtbl_lock. */
60     unsigned long rss;
61 };
62 
63 /* Interfaces on vmspace management */
64 int vmspace_init(struct vmspace *vmspace, unsigned long pcid);
65 void vmspace_deinit(void *ptr);
66 void plat_vmspace_init(struct vmspace *vmspace);
67 int vmspace_map_range(struct vmspace *vmspace, vaddr_t va, size_t len,
68                       vmr_prop_t flags, struct pmobject *pmo);
69 int vmspace_unmap_range(struct vmspace *vmspace, vaddr_t va, size_t len);
70 int unmap_pmo_in_vmspace(struct vmspace *vmspace, struct pmobject *pmo);
71 struct vmregion *find_vmr_for_va(struct vmspace *vmspace, vaddr_t addr);
72 int trans_uva_to_kva(vaddr_t user_va, vaddr_t *kernel_va);
73 
74 /* Two interfaces on heap management */
75 struct vmregion *init_heap_vmr(struct vmspace *vmspace, vaddr_t va,
76                                struct pmobject *pmo);
77 void adjust_heap_vmr(struct vmspace *vmspace, unsigned long len);
78 
79 /* Print all the vmrs inside one vmspace */
80 void kprint_vmr(struct vmspace *vmspace);
81 
82 /* For TLB maintenence */
83 void record_history_cpu(struct vmspace *vmspcae, unsigned int cpuid);
84 void clear_history_cpu(struct vmspace *vmspcae, unsigned int cpuid);
85 
86 /* The following functions' implementation is arch-dependent */
87 void switch_vmspace_to(struct vmspace *);
88 void arch_vmspace_init(struct vmspace *);
89 void free_page_table(void *);
90 struct vmspace *create_idle_vmspace(void);
91 
92 /* Interfaces on CoW implementation */
93 void vmregion_record_cow_private_page(struct vmregion *vmr, void *private_page);
94 
95 #endif /* MM_VMSPACE_H */