diff --git a/arch/x86/mm/memory.c b/arch/x86/mm/memory.c index 67bcd8f59..629afc022 100644 --- a/arch/x86/mm/memory.c +++ b/arch/x86/mm/memory.c @@ -39,6 +39,9 @@ #define GAP_BELOW 0x100000ULL +#define IB_MEMORY_SIZE (1UL << 20) +#define IB_MEMORY_NPAGES (IB_MEMORY_SIZE / PAGE_SIZE) + extern uint64_t base; extern uint64_t limit; @@ -369,3 +372,14 @@ oom: LOG_ERROR("BUG: Failed to init mm!\n"); while(1) {HALT; } } + +int ib_memory_init(void) +{ + size_t phyaddr, viraddr, bits; + + /* phyaddr = */ + viraddr = vma_alloc(IB_MEMORY_NPAGES * PAGE_SIZE, VMA_READ|VMA_WRITE|VMA_CACHEABLE); + + + +} diff --git a/kernel/ibv.c b/kernel/ibv.c index 98fca300e..9a87c85d0 100644 --- a/kernel/ibv.c +++ b/kernel/ibv.c @@ -55,7 +55,8 @@ typedef struct { // Parameters: int * num_devices; // Return value: - struct ibv_device * ret[MAX_NUM_OF_IBV_DEVICES]; + /* struct ibv_device * ret[MAX_NUM_OF_IBV_DEVICES]; */ + struct ibv_device ** ret; } __attribute__((packed)) uhyve_ibv_get_device_list_t; struct ibv_device ** ibv_get_device_list(int * num_devices) { @@ -64,22 +65,22 @@ struct ibv_device ** ibv_get_device_list(int * num_devices) { uhyve_args.num_devices = (int *) guest_to_host((size_t) num_devices); // Allocate memory for return value. - struct ibv_device * devs = kmalloc(MAX_NUM_OF_IBV_DEVICES * sizeof(struct ibv_device)); - struct ibv_device ** ret_guest = kmalloc(MAX_NUM_OF_IBV_DEVICES * sizeof(struct ibv_device *)); + /* struct ibv_device * devs = kmalloc(MAX_NUM_OF_IBV_DEVICES * sizeof(struct ibv_device)); */ + /* struct ibv_device ** ret_guest = kmalloc(MAX_NUM_OF_IBV_DEVICES * sizeof(struct ibv_device *)); */ // We keep a list of the virtual addresses, so we can return it later, and map // to physical addresses for the args struct passed to uhyve. - for (int i = 0; i < MAX_NUM_OF_IBV_DEVICES; i++) { - struct ibv_device * device_address = devs + i; - ret_guest[i] = device_address; - uhyve_args.ret[i] = (struct ibv_device *) guest_to_host((size_t) device_address); - } + /* for (int i = 0; i < MAX_NUM_OF_IBV_DEVICES; i++) { */ + /* struct ibv_device * device_address = devs + i; */ + /* ret_guest[i] = device_address; */ + /* uhyve_args.ret[i] = (struct ibv_device *) guest_to_host((size_t) device_address); */ + /* } */ uhyve_send(UHYVE_PORT_IBV_GET_DEVICE_LIST, (unsigned) virt_to_phys((size_t) &uhyve_args)); - for (int i = 0; i < MAX_NUM_OF_IBV_DEVICES; i++) { - host_to_guest_ibv_device(ret_guest[i], GUEST); - } + /* for (int i = 0; i < MAX_NUM_OF_IBV_DEVICES; i++) { */ + /* host_to_guest_ibv_device(ret_guest[i], GUEST); */ + /* } */ return ret_guest; } @@ -105,10 +106,7 @@ const char * ibv_get_device_name(struct ibv_device * device) { host_to_guest_ibv_device(device, GUEST); ret_guest = host_to_guest((size_t) uhyve_args.ret); - LOG_INFO("LOG TEST\n"); return (char *) ret_guest; - - /* return device->name; // TODO: hack for testing */ } diff --git a/kernel/ibv_guest_host.c b/kernel/ibv_guest_host.c index e36896489..4b606e356 100644 --- a/kernel/ibv_guest_host.c +++ b/kernel/ibv_guest_host.c @@ -43,7 +43,7 @@ struct ibv_device * guest_to_host_ibv_device(struct ibv_device * device) { return (struct ibv_device *) guest_to_host((size_t) device); } -struct ibv_device * host_to_guest_ibv_device(struct ibv_device * device, addr_type type) { +struct ibv_device * host_to_guest_ibv_device(struct ibv_device * device, addr_type type) { struct ibv_device * vaddr = (type == GUEST) ? device : (struct ibv_device *) host_to_guest((size_t) device); @@ -66,7 +66,7 @@ struct ibv_context * guest_to_host_ibv_context(struct ibv_context * context) { } struct ibv_context * host_to_guest_ibv_context(struct ibv_context * context, addr_type type) { - struct ibv_context * vaddr = (type == GUEST) ? context + struct ibv_context * vaddr = (type == GUEST) ? context : (struct ibv_context *) host_to_guest((size_t) context); vaddr->device = host_to_guest_ibv_device(vaddr->device, HOST); @@ -96,7 +96,7 @@ struct ibv_port_attr * host_to_guest_ibv_port_attr(struct ibv_port_attr * port_a /* - * struct ibv_comp_channel + * struct ibv_comp_channel */ struct ibv_comp_channel * guest_to_host_ibv_comp_channel(struct ibv_comp_channel * channel) { @@ -127,7 +127,7 @@ struct ibv_abi_compat_v2 * guest_to_host_ibv_abi_compat_v2(struct ibv_abi_compat } struct ibv_abi_compat_v2 * host_to_guest_ibv_abi_compat_v2(struct ibv_abi_compat_v2 * abi_compat, addr_type type) { - struct ibv_abi_compat_v2 * vaddr = (type == GUEST) ? abi_compat + struct ibv_abi_compat_v2 * vaddr = (type == GUEST) ? abi_compat : (struct ibv_abi_compat_v2 *) host_to_guest((size_t) abi_compat); host_to_guest_ibv_comp_channel(&abi_compat->channel, GUEST); diff --git a/kernel/main.c b/kernel/main.c index ece3fcbff..36ee162b6 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -122,6 +122,7 @@ static int hermit_init(void) timer_init(); multitasking_init(); memory_init(); + ib_memory_init(); signal_init(); return 0; diff --git a/tools/uhyve-ibv.h b/tools/uhyve-ibv.h index bd814e343..efc3181fa 100644 --- a/tools/uhyve-ibv.h +++ b/tools/uhyve-ibv.h @@ -34,11 +34,6 @@ typedef enum { } uhyve_ibv_t; -//inline unsigned get_data(struct kvm_run * run) { - //return *((unsigned*)((size_t)run+run->io.data_offset)); -//} - - typedef struct { // Parameters: int * num_devices; diff --git a/tools/uhyve.c b/tools/uhyve.c index f9666bec5..68a1fce2b 100644 --- a/tools/uhyve.c +++ b/tools/uhyve.c @@ -62,6 +62,7 @@ #include #include #include +#include #include // Linux include @@ -193,6 +194,66 @@ static __thread struct kvm_run *run = NULL; static __thread int vcpufd = -1; static __thread uint32_t cpuid = 0; +static bool ib_malloc = false; +static uint8_t * ib_mem = NULL; + +// Definition of malloc hooks for IBV library +static void ib_init_hook(void); +static void * ib_malloc_hook(size_t, const void *); +static void ib_free_hook(void *, const void *); + +static void * (* default_malloc_hook)(size_t, const void *); +static void (* default_free_hook)(void *, const void *); + +/* void (* __malloc_initialize_hook) (void) = ib_init_hook; */ +/* __malloc_initialize_hook = ib_init_hook; */ + +static void ib_init_hook(void) { + default_malloc_hook = __malloc_hook; + default_free_hook = __free_hook; + + __malloc_hook = ib_malloc_hook; + __free_hook = ib_free_hook; +} + +static void * ib_malloc_hook(size_t size, const void * caller) { + void * result; + + __malloc_hook = default_malloc_hook; + __free_hook = default_free_hook; + + if (ib_malloc) { + ib_mem -= size; + result = ib_mem; + } else { + result = malloc(size); + } + + default_malloc_hook = __malloc_hook; + default_free_hook = __free_hook; + + __malloc_hook = ib_malloc_hook; + __free_hook = ib_free_hook; + + return result; +} + +static void ib_free_hook(void * ptr, const void * caller) { + __malloc_hook = default_malloc_hook; + __free_hook = default_free_hook; + + if (!ib_malloc) { + free(ptr); + } + + default_malloc_hook = __malloc_hook; + default_free_hook = __free_hook; + + __malloc_hook = ib_malloc_hook; + __free_hook = ib_free_hook; +} + + static uint64_t memparse(const char *ptr) { // local pointer to end of parsed string @@ -1431,6 +1492,9 @@ int uhyve_init(char *path) err(1, "unable to initialized network"); } + ib_mem = guest_mem + guest_size; + ib_init_hook(); + return ret; } diff --git a/usr/tests/ib-test.c b/usr/tests/ib-test.c index a51bda8bb..1ae9d05b9 100644 --- a/usr/tests/ib-test.c +++ b/usr/tests/ib-test.c @@ -59,13 +59,15 @@ int main(int argc, char** argv) printf("after get device list -- ib-test.c: name 1: %s\n", dev_list[0]->name); printf("after get device list -- ib-test.c: name 2: %s\n", dev_list[1]->name); - printf("before get device name.\n"); - const char* dev_name = ibv_get_device_name(dev_list[0]); - printf("after get device name -- Device: %s\n", dev_name); + printf("before get device name loop.\n"); + for (int i=0; i < num_devices; i++) { + const char* dev_name = ibv_get_device_name(dev_list[i]); + printf("after get device name -- Device name %d: %s\n", i, dev_name); + } printf("before open_device\n"); struct ibv_context * context = ibv_open_device(dev_list[0]); - printf("after open device name -- Device: %s\n", dev_name); + printf("after open device\n");