#include <palacios/vmm_dev_mgr.h>
#include <palacios/vmm_emulator.h>
#include <palacios/vm_guest_mem.h>
+#include <palacios/vmm_io.h>
-#include <devices/cga.h>
+#include <devices/console.h>
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];
}
}
-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);
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;
-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;
-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;
}
-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) {
}
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;
}
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;
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;