2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2011, Kyle C. Hale <kh@u.norhtwestern.edu>
11 * Copyright (c) 2011, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Kyle C. Hale <kh@u.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm.h>
21 #include <palacios/vm_guest_mem.h>
22 #include <palacios/vm_guest.h>
23 #include <palacios/vmm_intr.h>
24 #include <palacios/vmm_decoder.h>
25 #include <palacios/vmm_string.h>
26 #include <palacios/vmm_shadow_paging.h>
27 #include <palacios/vmm_syscall_hijack.h>
28 #include <palacios/vmm_linux_syscall_map.h>
29 #include <palacios/vmm_process_environment.h>
30 #include <palacios/vmm_execve_hook.h>
33 #ifndef CONFIG_DEBUG_SYSCALL_HIJACK
35 #define PrintDebug(fmt, args...)
40 #define max(a, b) ( ((a) > (b)) ? (a) : (b) )
45 static void print_arg (struct guest_info * core, v3_reg_t reg, uint8_t argnum) {
50 PrintDebug("\t ARG%d: INT - %ld\n", argnum, (long) reg);
52 if (core->mem_mode == PHYSICAL_MEM) {
53 ret = v3_gpa_to_hva(core, get_addr_linear(core, reg, &(core->segments.ds)), &hva);
56 ret = v3_gva_to_hva(core, get_addr_linear(core, reg, &(core->segments.ds)), &hva);
59 PrintDebug("\t STR - ");
65 uint32_t c = max(MAX_CHARS, 4096 - (hva % 4096));
67 for (; i < c && *((char*)(hva + i)) != 0; i++) {
68 PrintDebug("%c", *((char*)(hva + i)));
74 static void print_syscall (uint8_t is64, struct guest_info * core) {
77 PrintDebug("Syscall #%ld: \"%s\"\n", (long)core->vm_regs.rax, get_linux_syscall_name64(core->vm_regs.rax));
79 PrintDebug("Syscall #%ld: \"%s\"\n", (long)core->vm_regs.rax, get_linux_syscall_name32(core->vm_regs.rax));
82 print_arg(core, core->vm_regs.rbx, 1);
83 print_arg(core, core->vm_regs.rcx, 2);
84 print_arg(core, core->vm_regs.rdx, 3);
88 int v3_syscall_handler (struct guest_info * core, uint8_t vector, void * priv_data) {
90 uint_t syscall_nr = (uint_t) core->vm_regs.rax;
93 struct v3_syscall_hook * hook = core->sc_hook_map.syscall_hooks[syscall_nr];
95 #ifdef CONFIG_SYSCALL_PASSTHROUGH
96 if (v3_hook_passthrough_syscall(core, syscall_nr) == -1) {
97 PrintDebug("Error hooking passthrough syscall\n");
100 hook = core->sc_hook_map.syscall_hooks[syscall_nr];
102 return v3_signal_swintr(core, vector);
106 err = hook->handler(core, syscall_nr, hook->priv_data);
108 PrintDebug("V3 Syscall Handler: Error in syscall hook\n");
116 static inline struct v3_syscall_hook * get_syscall_hook (struct guest_info * core, uint_t syscall_nr) {
117 return core->sc_hook_map.syscall_hooks[syscall_nr];
121 int v3_hook_syscall (struct guest_info * core,
123 int (*handler)(struct guest_info * core, uint_t syscall_nr, void * priv_data),
126 struct v3_syscall_hook * hook = (struct v3_syscall_hook *)V3_Malloc(sizeof(struct v3_syscall_hook));
133 if (get_syscall_hook(core, syscall_nr) != NULL) {
134 PrintError("System Call #%d already hooked\n", syscall_nr);
138 hook->handler = handler;
139 hook->priv_data = priv_data;
141 core->sc_hook_map.syscall_hooks[syscall_nr] = hook;
147 static int passthrough_syscall_handler (struct guest_info * core, uint_t syscall_nr, void * priv_data) {
148 print_syscall(0, core);
153 int v3_hook_passthrough_syscall (struct guest_info * core, uint_t syscall_nr) {
155 int rc = v3_hook_syscall(core, syscall_nr, passthrough_syscall_handler, NULL);
158 PrintError("failed to hook syscall 0x%x for passthrough (guest=0x%p)\n", syscall_nr, (void *)core);
161 PrintDebug("hooked syscall 0x%x for passthrough (guest=0x%p)\n", syscall_nr, (void *)core);
165 /* shouldn't get here */
170 int v3_sysexecve_handler (struct guest_info * core, uint_t syscall_nr, void * priv_data) {
172 struct exec_hook * hook;
175 ret = v3_gva_to_hva(core, get_addr_linear(core, (addr_t)core->vm_regs.rbx, &(core->segments.ds)), &hva);
177 PrintDebug("Error translating file path in sysexecve handler\n");
181 key = v3_hash_buffer((uchar_t*)hva, strlen((uchar_t*)hva));
182 if ((hook = (struct exec_hook*)v3_htable_search(core->exec_hooks.bin_table, key)) != NULL) {
183 if (hook->handler(core, NULL) == -1) {
184 PrintDebug("Error handling execve hook\n");