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[DBG] %s : thread %x enter / process %x / cycle %d\n", |
---|
57 | __FUNCTION__ , this, process->pid, (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 | // enable IRQs |
---|
75 | hal_enable_irq( &save_sr ); |
---|
76 | |
---|
77 | // call relevant kernel function |
---|
78 | error = vmm_resize_vseg( process , (intptr_t)vaddr , (intptr_t)size ); |
---|
79 | |
---|
80 | if ( error ) |
---|
81 | { |
---|
82 | |
---|
83 | #if DEBUG_SYSCALLS_ERROR |
---|
84 | printk("\n[ERROR] in %s : cannot remove mapping\n", __FUNCTION__ ); |
---|
85 | #endif |
---|
86 | this->errno = EINVAL; |
---|
87 | return -1; |
---|
88 | } |
---|
89 | |
---|
90 | // restore IRQs |
---|
91 | hal_restore_irq( save_sr ); |
---|
92 | |
---|
93 | #if (DEBUG_SYS_MUNMAP || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
94 | uint64_t tm_end = hal_get_cycles(); |
---|
95 | #endif |
---|
96 | |
---|
97 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
98 | hal_atomic_add( &syscalls_cumul_cost[SYS_MUNMAP] , tm_end - tm_start ); |
---|
99 | hal_atomic_add( &syscalls_occurences[SYS_MUNMAP] , 1 ); |
---|
100 | #endif |
---|
101 | |
---|
102 | #if DEBUG_SYS_MUNMAP |
---|
103 | if( DEBUG_SYS_MUNMAP < tm_start ) |
---|
104 | printk("\n[DBG] %s : thread %x exit / process %x / cycle %d\n", |
---|
105 | __FUNCTION__ , this, process->pid, (uint32_t)tm_end ); |
---|
106 | #endif |
---|
107 | |
---|
108 | return 0; |
---|
109 | |
---|
110 | } // end sys_munmap() |
---|
111 | |
---|