X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fdevices%2Fcga.c;h=0e36c9cf85feaa5367b7164a91753d4d529b898b;hb=bc5ee3e07affac4810227d61c407011c05298518;hp=aabe81072351e785a9829fec4bdbe90751ccfd25;hpb=b54cbe1dbbe15ed228de7472ea284df96ecce909;p=palacios.git diff --git a/palacios/src/devices/cga.c b/palacios/src/devices/cga.c index aabe810..0e36c9c 100644 --- a/palacios/src/devices/cga.c +++ b/palacios/src/devices/cga.c @@ -23,17 +23,18 @@ #include #include #include +#include + +#include -#include -/* #ifndef DEBUG_CGA #undef PrintDebug #define PrintDebug(fmt, args...) #endif -*/ + #define START_ADDR 0xB8000 #define END_ADDR 0xC0000 @@ -51,6 +52,7 @@ struct video_internal { uint8_t * framebuf; + addr_t framebuf_pa; // These store the values for unhandled ports, in case of a read op uint8_t port_store[44]; @@ -58,9 +60,11 @@ struct video_internal { uint8_t crtc_index_reg; // io port 3D4 uint8_t crtc_data_regs[25]; // io port 3D5 - + + /* IMPORTANT: These are column offsets _NOT_ byte offsets */ uint16_t screen_offset; // relative to the framebuffer uint16_t cursor_offset; // relative to the framebuffer + /* ** */ // updating the screen offset is not atomic, // so we need a temp variable to hold the partial update @@ -113,11 +117,12 @@ static void passthrough_out(uint16_t port, void * src, uint_t length) { } } -static int video_write_mem(addr_t guest_addr, void * dest, uint_t length, void * priv_data) { +static int video_write_mem(struct guest_info * core, addr_t guest_addr, void * dest, uint_t length, void * priv_data) { struct vm_device * dev = (struct vm_device *)priv_data; struct video_internal * state = (struct video_internal *)dev->private_data; uint_t fb_offset = guest_addr - START_ADDR; uint_t screen_byte_offset = state->screen_offset * BYTES_PER_COL; + uint_t screen_length; PrintDebug("Guest address: %p length = %d, fb_offset=%d, screen_offset=%d\n", (void *)guest_addr, length, fb_offset, screen_byte_offset); @@ -134,14 +139,19 @@ static int video_write_mem(addr_t guest_addr, void * dest, uint_t length, void * if (state->ops) { PrintDebug("\tcalling update_screen()\n"); - state->ops->update_screen(x, y, length, state->private_data); + + /* avoid updates past end of screen */ + screen_length = SCREEN_SIZE - screen_byte_offset; + if (screen_length > length) screen_length = length; + state->ops->update_screen(x, y, screen_length, state->private_data); } } return length; } -static int video_read_port(uint16_t port, void * dest, uint_t length, struct vm_device * dev) { +static int video_read_port(struct guest_info * core, uint16_t port, void * dest, uint_t length, void * priv_data) { + struct vm_device * dev = (struct vm_device *)priv_data; struct video_internal * video_state = (struct video_internal *)dev->private_data; @@ -156,7 +166,8 @@ static int video_read_port(uint16_t port, void * dest, uint_t length, struct vm_ -static int video_write_port(uint16_t port, void * src, uint_t length, struct vm_device * dev) { +static int video_write_port(struct guest_info * core, uint16_t port, void * src, uint_t length, void * priv_data) { + struct vm_device * dev = (struct vm_device *)priv_data; struct video_internal * video_state = (struct video_internal *)dev->private_data; @@ -171,7 +182,8 @@ static int video_write_port(uint16_t port, void * src, uint_t length, struct vm_ -static int crtc_data_write(uint16_t port, void * src, uint_t length, struct vm_device * dev) { +static int crtc_data_write(struct guest_info * core, uint16_t port, void * src, uint_t length, void * priv_data) { + struct vm_device * dev = (struct vm_device *)priv_data; struct video_internal * video_state = (struct video_internal *)dev->private_data; uint8_t val = *(uint8_t *)src; uint_t index = video_state->crtc_index_reg; @@ -250,7 +262,8 @@ static int crtc_data_write(uint16_t port, void * src, uint_t length, struct vm_d } -static int crtc_index_write(uint16_t port, void * src, uint_t length, struct vm_device * dev) { +static int crtc_index_write(struct guest_info * core, uint16_t port, void * src, uint_t length, void * priv_data) { + struct vm_device * dev = (struct vm_device *)priv_data; struct video_internal * video_state = (struct video_internal *)dev->private_data; if (length > 2) { @@ -269,7 +282,7 @@ static int crtc_index_write(uint16_t port, void * src, uint_t length, struct vm_ } if (length == 2) { - if (crtc_data_write(port + 1, src + 1, length - 1, dev) != (length - 1)) { + if (crtc_data_write(core, port + 1, src + 1, length - 1, dev) != (length - 1)) { PrintError("could not handle implicit crtc data write\n"); return -1; } @@ -287,6 +300,10 @@ int v3_cons_get_fb(struct vm_device * frontend_dev, uint8_t * dst, uint_t offset PrintDebug("Getting framebuffer for screen; framebuf=%p, screen_offset=%d, offset=%d, length=%d\n", state->framebuf, screen_byte_offset, offset, length); + V3_ASSERT(screen_byte_offset <= FRAMEBUF_SIZE - SCREEN_SIZE); + V3_ASSERT(offset < SCREEN_SIZE); + V3_ASSERT(length <= SCREEN_SIZE); + V3_ASSERT(offset + length <= SCREEN_SIZE); memcpy(dst, state->framebuf + screen_byte_offset + offset, length); return 0; @@ -295,103 +312,158 @@ int v3_cons_get_fb(struct vm_device * frontend_dev, uint8_t * dst, uint_t offset static int free_device(struct vm_device * dev) { - v3_unhook_mem(dev->vm, START_ADDR); + struct video_internal * video_state = (struct video_internal *)dev->private_data; + struct v3_vm_info * vm = dev->vm; + + if (video_state->framebuf_pa) { + V3_FreePages((void *)(video_state->framebuf_pa), (FRAMEBUF_SIZE / 4096)); + } + + v3_unhook_mem(dev->vm, V3_MEM_CORE_ANY, START_ADDR); + + v3_unhook_io_port(vm, 0x3b0); + v3_unhook_io_port(vm, 0x3b1); + v3_unhook_io_port(vm, 0x3b2); + v3_unhook_io_port(vm, 0x3b3); + v3_unhook_io_port(vm, 0x3b4); + v3_unhook_io_port(vm, 0x3b5); + v3_unhook_io_port(vm, 0x3b6); + v3_unhook_io_port(vm, 0x3b7); + v3_unhook_io_port(vm, 0x3b8); + v3_unhook_io_port(vm, 0x3b9); + v3_unhook_io_port(vm, 0x3ba); + v3_unhook_io_port(vm, 0x3bb); + v3_unhook_io_port(vm, 0x3c0); + v3_unhook_io_port(vm, 0x3c1); + v3_unhook_io_port(vm, 0x3c2); + v3_unhook_io_port(vm, 0x3c3); + v3_unhook_io_port(vm, 0x3c4); + v3_unhook_io_port(vm, 0x3c5); + v3_unhook_io_port(vm, 0x3c6); + v3_unhook_io_port(vm, 0x3c7); + v3_unhook_io_port(vm, 0x3c8); + v3_unhook_io_port(vm, 0x3c9); + v3_unhook_io_port(vm, 0x3ca); + v3_unhook_io_port(vm, 0x3cb); + v3_unhook_io_port(vm, 0x3cc); + v3_unhook_io_port(vm, 0x3cd); + v3_unhook_io_port(vm, 0x3ce); + v3_unhook_io_port(vm, 0x3cf); + v3_unhook_io_port(vm, 0x3d0); + v3_unhook_io_port(vm, 0x3d1); + v3_unhook_io_port(vm, 0x3d2); + v3_unhook_io_port(vm, 0x3d3); + v3_unhook_io_port(vm, 0x3d4); + v3_unhook_io_port(vm, 0x3d5); + v3_unhook_io_port(vm, 0x3d6); + v3_unhook_io_port(vm, 0x3d7); + v3_unhook_io_port(vm, 0x3d8); + v3_unhook_io_port(vm, 0x3d9); + v3_unhook_io_port(vm, 0x3da); + v3_unhook_io_port(vm, 0x3db); + v3_unhook_io_port(vm, 0x3dc); + v3_unhook_io_port(vm, 0x3dd); + v3_unhook_io_port(vm, 0x3de); + v3_unhook_io_port(vm, 0x3df); + + V3_Free(video_state); + return 0; } - static struct v3_device_ops dev_ops = { .free = free_device, - .reset = NULL, - .start = NULL, - .stop = NULL, }; -static int cga_init(struct guest_info * vm, void * cfg_data) { - struct video_internal * video_state = (struct video_internal *)V3_Malloc(sizeof(struct video_internal)); - addr_t frame_buf_pa = 0; - uint32_t enable_passthrough = (uint32_t)(addr_t)cfg_data; - - +static int cga_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) { + struct video_internal * video_state = NULL; + char * dev_id = v3_cfg_val(cfg, "ID"); + char * passthrough_str = v3_cfg_val(cfg, "passthrough"); + PrintDebug("video: init_device\n"); - struct vm_device * dev = v3_allocate_device("CGA_VIDEO", &dev_ops, video_state); + video_state = (struct video_internal *)V3_Malloc(sizeof(struct video_internal)); + memset(video_state, 0, sizeof(struct video_internal)); + + struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, video_state); if (v3_attach_device(vm, dev) == -1) { - PrintError("Could not attach device %s\n", "CGA_VIDEO"); + PrintError("Could not attach device %s\n", dev_id); + V3_Free(video_state); return -1; } - frame_buf_pa = (addr_t)V3_AllocPages(FRAMEBUF_SIZE / 4096); - - video_state->framebuf = V3_VAddr((void *)frame_buf_pa); + video_state->framebuf_pa = (addr_t)V3_AllocPages(FRAMEBUF_SIZE / 4096); + video_state->framebuf = V3_VAddr((void *)(video_state->framebuf_pa)); memset(video_state->framebuf, 0, FRAMEBUF_SIZE); - PrintDebug("PA of array: %p\n", (void *)frame_buf_pa); + PrintDebug("PA of array: %p\n", (void *)(video_state->framebuf_pa)); - video_state->passthrough = enable_passthrough; + if ((passthrough_str != NULL) && + (strcasecmp(passthrough_str, "enable") == 0)) {; + video_state->passthrough = 1; + } - video_state->ops = NULL; - video_state->private_data = NULL; - if (enable_passthrough) { + if (video_state->passthrough) { PrintDebug("Enabling CGA Passthrough\n"); - - if (v3_hook_write_mem(vm, START_ADDR, END_ADDR, START_ADDR, &video_write_mem, dev) == -1) { + if (v3_hook_write_mem(vm, V3_MEM_CORE_ANY, START_ADDR, END_ADDR, + START_ADDR, &video_write_mem, dev) == -1) { PrintDebug("\n\nVideo Hook failed.\n\n"); } } else { - if (v3_hook_write_mem(vm, START_ADDR, END_ADDR, frame_buf_pa, &video_write_mem, dev) == -1) { + if (v3_hook_write_mem(vm, V3_MEM_CORE_ANY, START_ADDR, END_ADDR, + video_state->framebuf_pa, &video_write_mem, dev) == -1) { PrintDebug("\n\nVideo Hook failed.\n\n"); } } - v3_dev_hook_io(dev, 0x3b0, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3b1, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3b2, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3b3, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3b4, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3b5, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3b6, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3b7, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3b8, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3b9, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3ba, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3bb, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3c0, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3c1, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3c2, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3c3, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3c4, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3c5, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3c6, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3c7, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3c8, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3c9, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3ca, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3cb, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3cc, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3cd, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3ce, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3cf, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3d0, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3d1, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3d2, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3d3, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3d4, &video_read_port, &crtc_index_write); - v3_dev_hook_io(dev, 0x3d5, &video_read_port, &crtc_data_write); - v3_dev_hook_io(dev, 0x3d6, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3d7, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3d8, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3d9, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3da, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3db, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3dc, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3dd, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3de, &video_read_port, &video_write_port); - v3_dev_hook_io(dev, 0x3df, &video_read_port, &video_write_port); + v3_hook_io_port(vm, 0x3b0, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3b1, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3b2, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3b3, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3b4, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3b5, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3b6, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3b7, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3b8, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3b9, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3ba, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3bb, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3c0, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3c1, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3c2, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3c3, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3c4, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3c5, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3c6, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3c7, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3c8, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3c9, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3ca, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3cb, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3cc, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3cd, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3ce, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3cf, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3d0, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3d1, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3d2, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3d3, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3d4, &video_read_port, &crtc_index_write, dev); + v3_hook_io_port(vm, 0x3d5, &video_read_port, &crtc_data_write, dev); + v3_hook_io_port(vm, 0x3d6, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3d7, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3d8, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3d9, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3da, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3db, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3dc, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3dd, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3de, &video_read_port, &video_write_port, dev); + v3_hook_io_port(vm, 0x3df, &video_read_port, &video_write_port, dev); return 0;