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_extensions.h>
29 #include <interfaces/syscall_hijack.h>
30 #include <interfaces/sw_intr.h>
32 #include "syscall_ref.h"
34 #ifndef V3_CONFIG_DEBUG_EXT_SYSCALL_HIJACK
36 #define PrintDebug(fmt, args...)
41 #define max(a, b) ( ((a) > (b)) ? (a) : (b) )
44 #define SYSCALL_INT_VECTOR 0x80
47 struct v3_syscall_hook {
48 int (*handler)(struct guest_info * core, uint_t syscall_nr, void * priv_data);
52 static struct v3_syscall_hook * syscall_hooks[512];
55 static int v3_syscall_handler (struct guest_info * core, uint8_t vector, void * priv_data) {
57 uint_t syscall_nr = (uint_t) core->vm_regs.rax;
60 struct v3_syscall_hook * hook = syscall_hooks[syscall_nr];
62 #ifdef V3_CONFIG_EXT_SYSCALL_PASSTHROUGH
63 if (v3_hook_passthrough_syscall(core, syscall_nr) == -1) {
64 PrintDebug("Error hooking passthrough syscall\n");
67 hook = syscall_hooks[syscall_nr];
69 return v3_signal_swintr(core, vector);
73 err = hook->handler(core, syscall_nr, hook->priv_data);
75 PrintDebug("V3 Syscall Handler: Error in syscall hook\n");
83 static int init_syscall_hijack (struct v3_vm_info * vm, v3_cfg_tree_t * cfg, void ** priv_data) {
89 static int init_syscall_hijack_core (struct guest_info * core, void * priv_data) {
91 v3_hook_swintr(core, SYSCALL_INT_VECTOR, v3_syscall_handler, NULL);
96 static void print_arg (struct guest_info * core, v3_reg_t reg, uint8_t argnum) {
101 PrintDebug("\t ARG%d: INT - %ld\n", argnum, (long) reg);
103 if (core->mem_mode == PHYSICAL_MEM) {
104 ret = v3_gpa_to_hva(core, get_addr_linear(core, reg, &(core->segments.ds)), &hva);
107 ret = v3_gva_to_hva(core, get_addr_linear(core, reg, &(core->segments.ds)), &hva);
110 PrintDebug("\t STR - ");
116 uint32_t c = max(MAX_CHARS, 4096 - (hva % 4096));
118 for (; i < c && *((char*)(hva + i)) != 0; i++) {
119 PrintDebug("%c", *((char*)(hva + i)));
125 static void print_syscall (uint8_t is64, struct guest_info * core) {
128 PrintDebug("Syscall #%ld: \"%s\"\n", (long)core->vm_regs.rax, get_linux_syscall_name64(core->vm_regs.rax));
130 PrintDebug("Syscall #%ld: \"%s\"\n", (long)core->vm_regs.rax, get_linux_syscall_name32(core->vm_regs.rax));
133 print_arg(core, core->vm_regs.rbx, 1);
134 print_arg(core, core->vm_regs.rcx, 2);
135 print_arg(core, core->vm_regs.rdx, 3);
141 static struct v3_extension_impl syscall_impl = {
142 .name = "syscall_intercept",
143 .init = init_syscall_hijack,
145 .core_init = init_syscall_hijack_core,
151 register_extension(&syscall_impl);
156 static inline struct v3_syscall_hook * get_syscall_hook (struct guest_info * core, uint_t syscall_nr) {
157 return syscall_hooks[syscall_nr];
161 int v3_hook_syscall (struct guest_info * core,
163 int (*handler)(struct guest_info * core, uint_t syscall_nr, void * priv_data),
166 struct v3_syscall_hook * hook = (struct v3_syscall_hook *)V3_Malloc(sizeof(struct v3_syscall_hook));
173 if (get_syscall_hook(core, syscall_nr) != NULL) {
174 PrintError("System Call #%d already hooked\n", syscall_nr);
178 hook->handler = handler;
179 hook->priv_data = priv_data;
181 syscall_hooks[syscall_nr] = hook;
187 static int passthrough_syscall_handler (struct guest_info * core, uint_t syscall_nr, void * priv_data) {
188 print_syscall(0, core);
193 int v3_hook_passthrough_syscall (struct guest_info * core, uint_t syscall_nr) {
195 int rc = v3_hook_syscall(core, syscall_nr, passthrough_syscall_handler, NULL);
198 PrintError("failed to hook syscall 0x%x for passthrough (guest=0x%p)\n", syscall_nr, (void *)core);
201 PrintDebug("hooked syscall 0x%x for passthrough (guest=0x%p)\n", syscall_nr, (void *)core);
205 /* shouldn't get here */
210 int v3_sysexecve_handler (struct guest_info * core, uint_t syscall_nr, void * priv_data) {
212 struct exec_hook * hook;
215 ret = v3_gva_to_hva(core, get_addr_linear(core, (addr_t)core->vm_regs.rbx, &(core->segments.ds)), &hva);
217 PrintDebug("Error translating file path in sysexecve handler\n");
221 key = v3_hash_buffer((uchar_t*)hva, strlen((uchar_t*)hva));
222 if ((hook = (struct exec_hook*)v3_htable_search(core->exec_hooks.bin_table, key)) != NULL) {
223 if (hook->handler(core, NULL) == -1) {
224 PrintDebug("Error handling execve hook\n");