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