1 | /* |
---|
2 | * sys_munmap.c - unmap a mapping from process virtual address space |
---|
3 | * |
---|
4 | * Authors Alain Greiner (2016,2017,2018,2019) |
---|
5 | * |
---|
6 | * Copyright (c) UPMC Sorbonne Universites |
---|
7 | * |
---|
8 | * This file is part of ALMOS-MKH. |
---|
9 | * |
---|
10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
11 | * under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 2.0 of the License. |
---|
13 | * |
---|
14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | * General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #include <hal_kernel_types.h> |
---|
25 | #include <hal_uspace.h> |
---|
26 | #include <hal_vmm.h> |
---|
27 | #include <hal_irqmask.h> |
---|
28 | #include <shared_syscalls.h> |
---|
29 | #include <errno.h> |
---|
30 | #include <thread.h> |
---|
31 | #include <printk.h> |
---|
32 | #include <mapper.h> |
---|
33 | #include <vfs.h> |
---|
34 | #include <process.h> |
---|
35 | #include <vmm.h> |
---|
36 | |
---|
37 | #include <syscalls.h> |
---|
38 | |
---|
39 | //////////////////////////////// |
---|
40 | int sys_munmap( void * vaddr, |
---|
41 | uint32_t size ) |
---|
42 | { |
---|
43 | error_t error; |
---|
44 | vseg_t * vseg; |
---|
45 | reg_t save_sr; // required to enable IRQs |
---|
46 | |
---|
47 | thread_t * this = CURRENT_THREAD; |
---|
48 | process_t * process = this->process; |
---|
49 | |
---|
50 | #if (DEBUG_SYS_MUNMAP || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
51 | uint64_t tm_start = hal_get_cycles(); |
---|
52 | #endif |
---|
53 | |
---|
54 | #if DEBUG_SYS_MUNMAP |
---|
55 | if( DEBUG_SYS_MUNMAP < tm_start ) |
---|
56 | printk("\n[%s] thread[%x,%x] enter / cycle %d\n", |
---|
57 | __FUNCTION__, process->pid, this->trdid, (uint32_t)tm_start ); |
---|
58 | #endif |
---|
59 | |
---|
60 | // check user buffer is mapped |
---|
61 | error = vmm_get_vseg( process , (intptr_t)vaddr, &vseg ); |
---|
62 | |
---|
63 | if( error ) |
---|
64 | { |
---|
65 | |
---|
66 | #if DEBUG_SYSCALLS_ERROR |
---|
67 | printk("\n[ERROR] in %s : thread[%x,%x] / user buffer unmapped %x\n", |
---|
68 | __FUNCTION__ , process->pid, this->trdid, (intptr_t)vaddr ); |
---|
69 | #endif |
---|
70 | this->errno = EINVAL; |
---|
71 | return -1; |
---|
72 | } |
---|
73 | |
---|
74 | // compute unmapped region min an max |
---|
75 | intptr_t addr_min = (intptr_t)vaddr; |
---|
76 | intptr_t addr_max = addr_min + size; |
---|
77 | |
---|
78 | |
---|
79 | // get vseg min & max addresses |
---|
80 | intptr_t vseg_min = vseg->min; |
---|
81 | intptr_t vseg_max = vseg->max; |
---|
82 | |
---|
83 | // enable IRQs |
---|
84 | hal_enable_irq( &save_sr ); |
---|
85 | |
---|
86 | // action depend on both vseg and region bases & sizes |
---|
87 | if( (vseg_min > addr_min) || (vseg_max < addr_max) ) // region not included in vseg |
---|
88 | { |
---|
89 | |
---|
90 | #if DEBUG_SYSCALLS_ERROR |
---|
91 | printk("\n[ERROR] in %s : region[%x->%x] / vseg[%x->%x] => non included in vseg\n", |
---|
92 | __FUNCTION__, process->pid, this->trdid, addr_min, addr_max, vseg_min, vseg_max ); |
---|
93 | #endif |
---|
94 | this->errno = EINVAL; |
---|
95 | return -1; |
---|
96 | } |
---|
97 | else if( (vseg_min == addr_min) && (vseg_max == addr_max) ) |
---|
98 | { |
---|
99 | |
---|
100 | #if( DEBUG_SYS_MUNMAP & 1 ) |
---|
101 | if( DEBUG_SYS_MUNMAP < tm_start ) |
---|
102 | printk("\n[%s] thread[%x,%x] unmapped region[%x->%x[ / vseg[%x->%x[ => delete vseg\n", |
---|
103 | __FUNCTION__, process->pid, this->trdid, addr_min, addr_max, vseg_min, vseg_max ); |
---|
104 | #endif |
---|
105 | // delete existing vseg |
---|
106 | vmm_global_delete_vseg( process, |
---|
107 | vseg_min ); |
---|
108 | } |
---|
109 | else if( (vseg_min == addr_min) || (vseg_max == addr_max) ) |
---|
110 | { |
---|
111 | |
---|
112 | #if( DEBUG_SYS_MUNMAP & 1 ) |
---|
113 | if( DEBUG_SYS_MUNMAP < tm_start ) |
---|
114 | printk("\n[%s] thread[%x,%x] unmapped region[%x->%x[ / vseg[%x->%x[ => resize vseg\n", |
---|
115 | __FUNCTION__, process->pid, this->trdid, addr_min, addr_max, vseg_min, vseg_max ); |
---|
116 | #endif |
---|
117 | // resize existing vseg |
---|
118 | vmm_global_resize_vseg( process, |
---|
119 | vseg_min, |
---|
120 | addr_min, |
---|
121 | addr_max - addr_min ); |
---|
122 | } |
---|
123 | else // addr_min > vseg_min) && (addr_max < vseg_max) |
---|
124 | { |
---|
125 | |
---|
126 | #if( DEBUG_SYS_MUNMAP & 1 ) |
---|
127 | if( DEBUG_SYS_MUNMAP < tm_start ) |
---|
128 | printk("\n[%s] thread[%x,%x] unmapped region[%x->%x[ / vseg[%x->%x[ => create new vseg\n", |
---|
129 | __FUNCTION__, process->pid, this->trdid, addr_min, addr_max, vseg_min, vseg_max ); |
---|
130 | #endif |
---|
131 | // resize existing vseg |
---|
132 | vmm_global_resize_vseg( process, |
---|
133 | vseg_min, |
---|
134 | vseg_min, |
---|
135 | addr_min - vseg_min ); |
---|
136 | |
---|
137 | // create new vseg |
---|
138 | vmm_create_vseg( process, |
---|
139 | vseg->type, |
---|
140 | addr_max, |
---|
141 | vseg_max - addr_max, |
---|
142 | vseg->file_offset, |
---|
143 | vseg->file_size, |
---|
144 | vseg->mapper_xp, |
---|
145 | vseg->cxy ); |
---|
146 | } |
---|
147 | |
---|
148 | // restore IRQs |
---|
149 | hal_restore_irq( save_sr ); |
---|
150 | |
---|
151 | #if (DEBUG_SYS_MUNMAP || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
152 | uint64_t tm_end = hal_get_cycles(); |
---|
153 | #endif |
---|
154 | |
---|
155 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
156 | hal_atomic_add( &syscalls_cumul_cost[SYS_MUNMAP] , tm_end - tm_start ); |
---|
157 | hal_atomic_add( &syscalls_occurences[SYS_MUNMAP] , 1 ); |
---|
158 | #endif |
---|
159 | |
---|
160 | #if DEBUG_SYS_MUNMAP |
---|
161 | if( DEBUG_SYS_MUNMAP < tm_start ) |
---|
162 | printk("\n[%s] thread [%x,%x] exit / cycle %d\n", |
---|
163 | __FUNCTION__ , process->pid, this->trdid, (uint32_t)tm_end ); |
---|
164 | #endif |
---|
165 | |
---|
166 | return 0; |
---|
167 | |
---|
168 | } // end sys_munmap() |
---|
169 | |
---|