Skip to content

Commit

Permalink
usermode: add mmap() and munmap() system calls
Browse files Browse the repository at this point in the history
Signed-off-by: Pawel Wieczorkiewicz <[email protected]>
  • Loading branch information
wipawel committed Jul 22, 2022
1 parent dd62f2e commit 067f393
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
61 changes: 61 additions & 0 deletions common/usermode.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
#include <lib.h>
#include <pagetable.h>
#include <percpu.h>
#include <processor.h>
#include <usermode.h>

Expand Down Expand Up @@ -92,6 +93,8 @@ void __naked syscall_handler(void) {
register unsigned long syscall_nr asm(STR(_ASM_AX));
register unsigned long param1 asm(STR(_ASM_DI));
(void) param1;
register unsigned long param2 asm(STR(_ASM_SI));
(void) param2;
SAVE_CLOBBERED_REGS();
switch_address_space(&cr3);
swapgs();
Expand All @@ -104,6 +107,27 @@ void __naked syscall_handler(void) {
_sys_exit();
UNREACHABLE();

case SYSCALL_MMAP: {
void *va = _ptr(param1);
unsigned int order = _u(param2);
frame_t *frame;

frame = get_free_frames(order);
if (!va)
va = mfn_to_virt_user(frame->mfn);

va = vmap_user(va, frame->mfn, order, L4_PROT_USER, L3_PROT_USER, L2_PROT_USER,
L1_PROT_USER);
syscall_return(_ul(va));
} break;

case SYSCALL_MUNMAP: {
void *va = _ptr(param1);
unsigned int order = _u(param2);

vunmap_user(va, order);
} break;

default:
printk("Unknown syscall: %lu\n", syscall_nr);
syscall_return(-1L);
Expand Down Expand Up @@ -152,6 +176,43 @@ static inline void __user_text sys_exit(unsigned long exit_code) {
asm volatile("syscall" ::"A"(SYSCALL_EXIT), "D"(exit_code) : STR(_ASM_CX), "r11");
}

static inline long __user_text sys_mmap(void *va, unsigned long order) {
register unsigned long rax asm(STR(_ASM_AX));

/* clang-format off */
asm volatile(
"syscall"
: "=A"(rax)
: "0"(SYSCALL_MMAP), "D"(va), "S"(order)
: STR(_ASM_CX), "r11"
);
/* clang-format on */

return rax;
}

static inline long __user_text sys_munmap(void *va, unsigned long order) {
register unsigned long rax asm(STR(_ASM_AX));

/* clang-format off */
asm volatile(
"syscall"
::"A"(SYSCALL_MUNMAP), "D"(va), "S"(order)
: STR(_ASM_CX), "r11"
);
/* clang-format on */

return rax;
}

void __user_text exit(unsigned long exit_code) {
sys_exit(exit_code);
}

void *__user_text mmap(void *va, unsigned long order) {
return _ptr(sys_mmap(va, order));
}

void __user_text munmap(void *va, unsigned long order) {
sys_munmap(va, order);
}
7 changes: 6 additions & 1 deletion include/usermode.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#ifndef KTF_USERMODE_H
#define KTF_USERMODE_H

#define SYSCALL_EXIT 0
#define SYSCALL_EXIT 0
#define SYSCALL_MMAP 2
#define SYSCALL_MUNMAP 3

#ifndef __ASSEMBLY__
#include <percpu.h>
Expand All @@ -51,6 +53,9 @@ extern void __naked syscall_handler(void);
extern void init_usermode(percpu_t *percpu);

extern void __user_text exit(unsigned long exit_code);
extern void *__user_text mmap(void *va, unsigned long order);
extern void __user_text munmap(void *va, unsigned long order);

#endif /* __ASSEMBLY__ */

#endif /* KTF_USERMODE_H */

0 comments on commit 067f393

Please sign in to comment.