Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Updated devices to remove vm_device dependencies.
[palacios.git] / palacios / src / devices / serial.c
index b527431..1ca3f13 100644 (file)
@@ -12,6 +12,7 @@
  * All rights reserved.
  *
  * Author: Rumou Duan <duanrumou@gmail.com>
+ *             Lei Xia <lxia@northwestern.edu>
  *
  * This is free software.  You are permitted to use,
  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
 #include <palacios/vmm_ringbuffer.h>
 #include <palacios/vmm_lock.h>
 #include <palacios/vmm_intr.h>
-#include <palacios/vmm_host_events.h>
 #include <palacios/vm_guest.h>
 
+#include <devices/serial.h>
+
 
 #ifndef CONFIG_DEBUG_SERIAL
 #undef PrintDebug
@@ -283,27 +285,30 @@ struct serial_port {
     struct serial_buffer tx_buffer;
     struct serial_buffer rx_buffer;
     uint_t irq_number;
+
+
+    void * backend_data;
+    struct v3_dev_char_ops * ops;
+
 };
 
 
 struct serial_state {
-    struct serial_port com1;
-    struct serial_port com2;
-    struct serial_port com3;
-    struct serial_port com4;
+    struct serial_port coms[4];
+
 };
 
 
 
 static struct serial_port * get_com_from_port(struct serial_state * serial, uint16_t port) {
     if ((port >= COM1_DATA_PORT) && (port <= COM1_SCRATCH_PORT)) {
-       return &(serial->com1);
+       return &(serial->coms[0]);
     } else if ((port >= COM2_DATA_PORT) && (port <= COM2_SCRATCH_PORT)) {
-       return &(serial->com2);
+       return &(serial->coms[1]);
     } else if ((port >= COM3_DATA_PORT) && (port <= COM3_SCRATCH_PORT)) {
-       return &(serial->com3);
+       return &(serial->coms[2]);
     } else if ((port >= COM4_DATA_PORT) && (port <= COM4_SCRATCH_PORT)) {
-       return &(serial->com4);
+       return &(serial->coms[3]);
     } else {
        PrintError("Error: Could not find serial port associated with IO port %d\n", port);
        return NULL;
@@ -338,7 +343,7 @@ static int getNumber(struct serial_buffer * buf) {
     }
 }
 
-static int updateIRQ(struct serial_port * com, struct vm_device * dev) {
+static int updateIRQ(struct v3_vm_info * vm, struct serial_port * com) {
     
     if ( (com->ier.erbfi == 0x1) && 
         (receive_buffer_trigger( getNumber(&(com->rx_buffer)), com->fcr.rx_trigger)) ) {
@@ -346,7 +351,7 @@ static int updateIRQ(struct serial_port * com, struct vm_device * dev) {
        PrintDebug("UART: receive buffer interrupt(trigger level reached)");
 
        com->iir.iid = RX_IRQ_TRIGGER_LEVEL;
-       v3_raise_irq(dev->vm, com->irq_number);
+       v3_raise_irq(vm, com->irq_number);
     }
     
     if ( (com->iir.iid == RX_IRQ_TRIGGER_LEVEL) && 
@@ -370,16 +375,15 @@ static int updateIRQ(struct serial_port * com, struct vm_device * dev) {
        com->iir.iid = TX_IRQ_THRE;
        com->iir.pending = 0;
 
-       v3_raise_irq(dev->vm, com->irq_number);
+       v3_raise_irq(vm, com->irq_number);
     }
 
     return 1;
 }
 
 
-static int queue_data(struct serial_buffer * buf, uint8_t  data, 
-                     struct serial_port * com, struct vm_device * dev) {
-
+static int queue_data(struct v3_vm_info * vm, struct serial_port * com,
+                     struct serial_buffer * buf, uint8_t data) {
     int next_loc = (buf->head + 1) % SERIAL_BUF_LEN;    
 
     if (buf->full == 1) {
@@ -408,13 +412,13 @@ static int queue_data(struct serial_buffer * buf, uint8_t  data,
        com->lsr.temt = 0;
     }
     
-    updateIRQ(com, dev);
+    updateIRQ(vm, com);
     
     return 0;
 }
 
-static int dequeue_data(struct serial_buffer * buf, uint8_t * data, 
-                       struct serial_port * com, struct vm_device * dev) {
+static int dequeue_data(struct v3_vm_info * vm, struct serial_port * com,
+                       struct serial_buffer * buf, uint8_t * data) {
 
     int next_tail = (buf->tail + 1) % SERIAL_BUF_LEN;
 
@@ -442,27 +446,14 @@ static int dequeue_data(struct serial_buffer * buf, uint8_t * data,
        com->lsr.temt = 1;
     }
     
-    updateIRQ(com, dev);
+    updateIRQ(vm, com);
     
     return 0;
 }
 
-/*
-static void printBuffer(struct serial_buffer * buf) {
-    int i = 0;
-
-    for (i = 0; i < SERIAL_BUF_LEN; i++) {
-       PrintDebug(" %d ", buf->buffer[i]);
-    }
-
-    PrintDebug("\n the number of elements is %d \n", getNumber(buf));
-}
-*/
-
-//note: write to data port is NOT implemented and corresponding codes are commented out
 static int write_data_port(struct guest_info * core, uint16_t port, 
-                          void * src, uint_t length, struct vm_device * dev) {
-    struct serial_state * state = (struct serial_state *)dev->private_data;
+                          void * src, uint_t length, void * priv_data) {
+    struct serial_state * state = priv_data;
     uint8_t * val = (uint8_t *)src;
     struct serial_port * com_port = NULL;
 
@@ -491,18 +482,24 @@ static int write_data_port(struct guest_info * core, uint16_t port,
     if (com_port->lcr.dlab == 1) {
        com_port->dll.data = *val;
     }  else {
-       queue_data(&(com_port->tx_buffer), *val, com_port, dev);
+       
+
+       /* JRL: Some buffering would probably be a good idea here.... */
+       if (com_port->ops) {
+           com_port->ops->write(val, 1, com_port->backend_data);
+       } else {
+           queue_data(core->vm_info, com_port, &(com_port->tx_buffer), *val);
+       }
     }
     
-    
     return length;
 }
 
 
 
 static int read_data_port(struct guest_info * core, uint16_t port, 
-                         void * dst, uint_t length, struct vm_device * dev) {
-    struct serial_state * state = (struct serial_state *)dev->private_data;
+                         void * dst, uint_t length, void * priv_data) {
+    struct serial_state * state = priv_data;
     uint8_t * val = (uint8_t *)dst;
     struct serial_port * com_port = NULL;
 
@@ -529,9 +526,9 @@ static int read_data_port(struct guest_info * core, uint16_t port,
     if (com_port->lcr.dlab == 1) {
        *val = com_port->dll.data;
     } else {
-       dequeue_data(&(com_port->rx_buffer), val, com_port, dev);
-    }
-    
+       dequeue_data(core->vm_info, com_port, &(com_port->rx_buffer), val);
+    }    
+       
     return length;
 }
 
@@ -584,8 +581,8 @@ static int handle_fcr_write(struct serial_port * com, uint8_t value) {
 
 
 static int write_ctrl_port(struct guest_info * core, uint16_t port, void * src, 
-                          uint_t length, struct vm_device * dev) {
-    struct serial_state * state = (struct serial_state *)dev->private_data;
+                          uint_t length, void * priv_data) {
+    struct serial_state * state = priv_data;
     uint8_t val = *(uint8_t *)src;
     struct serial_port * com_port = NULL;
 
@@ -668,8 +665,8 @@ static int write_ctrl_port(struct guest_info * core, uint16_t port, void * src,
 
 
 static int read_ctrl_port(struct guest_info * core, uint16_t port, void * dst, 
-                         uint_t length, struct vm_device * dev) {
-    struct serial_state * state = (struct serial_state *)dev->private_data;
+                         uint_t length, void * priv_data) {
+    struct serial_state * state = priv_data;
     uint8_t * val = (uint8_t *)dst;
     struct serial_port * com_port = NULL;
 
@@ -745,8 +742,8 @@ static int read_ctrl_port(struct guest_info * core, uint16_t port, void * dst,
 
 
 static int write_status_port(struct guest_info * core, uint16_t port, void * src, 
-                            uint_t length, struct vm_device * dev) {
-    struct serial_state * state = (struct serial_state *)dev->private_data;
+                            uint_t length, void * priv_data) {
+    struct serial_state * state = priv_data;
     uint8_t val = *(uint8_t *)src;
     struct serial_port * com_port = NULL;
 
@@ -790,8 +787,8 @@ static int write_status_port(struct guest_info * core, uint16_t port, void * src
 }
 
 static int read_status_port(struct guest_info * core, uint16_t port, void * dst, 
-                           uint_t length, struct vm_device * dev) {
-    struct serial_state * state = (struct serial_state *)dev->private_data;
+                           uint_t length, void * priv_data) {
+    struct serial_state * state = priv_data;
     uint8_t * val = (uint8_t *)dst;
     struct serial_port * com_port = NULL;
 
@@ -837,45 +834,7 @@ static int read_status_port(struct guest_info * core, uint16_t port, void * dst,
     return length;
 }
 
-static int serial_deinit(struct vm_device * dev) {
-
-
-    v3_dev_unhook_io(dev, COM1_DATA_PORT);
-    v3_dev_unhook_io(dev, COM1_IRQ_ENABLE_PORT);
-    v3_dev_unhook_io(dev, COM1_FIFO_CTRL_PORT);
-    v3_dev_unhook_io(dev, COM1_LINE_CTRL_PORT);
-    v3_dev_unhook_io(dev, COM1_MODEM_CTRL_PORT);
-    v3_dev_unhook_io(dev, COM1_LINE_STATUS_PORT);
-    v3_dev_unhook_io(dev, COM1_MODEM_STATUS_PORT);
-    v3_dev_unhook_io(dev, COM1_SCRATCH_PORT);
-
-    v3_dev_unhook_io(dev, COM2_DATA_PORT);
-    v3_dev_unhook_io(dev, COM2_IRQ_ENABLE_PORT);
-    v3_dev_unhook_io(dev, COM2_FIFO_CTRL_PORT);
-    v3_dev_unhook_io(dev, COM2_LINE_CTRL_PORT);
-    v3_dev_unhook_io(dev, COM2_MODEM_CTRL_PORT);
-    v3_dev_unhook_io(dev, COM2_LINE_STATUS_PORT);
-    v3_dev_unhook_io(dev, COM2_MODEM_STATUS_PORT);
-    v3_dev_unhook_io(dev, COM2_SCRATCH_PORT);
-
-    v3_dev_unhook_io(dev, COM3_DATA_PORT);
-    v3_dev_unhook_io(dev, COM3_IRQ_ENABLE_PORT);
-    v3_dev_unhook_io(dev, COM3_FIFO_CTRL_PORT);
-    v3_dev_unhook_io(dev, COM3_LINE_CTRL_PORT);
-    v3_dev_unhook_io(dev, COM3_MODEM_CTRL_PORT);
-    v3_dev_unhook_io(dev, COM3_LINE_STATUS_PORT);
-    v3_dev_unhook_io(dev, COM3_MODEM_STATUS_PORT);
-    v3_dev_unhook_io(dev, COM3_SCRATCH_PORT);
-
-    v3_dev_unhook_io(dev, COM4_DATA_PORT);
-    v3_dev_unhook_io(dev, COM4_IRQ_ENABLE_PORT);
-    v3_dev_unhook_io(dev, COM4_FIFO_CTRL_PORT);
-    v3_dev_unhook_io(dev, COM4_LINE_CTRL_PORT);
-    v3_dev_unhook_io(dev, COM4_MODEM_CTRL_PORT);
-    v3_dev_unhook_io(dev, COM4_LINE_STATUS_PORT);
-    v3_dev_unhook_io(dev, COM4_MODEM_STATUS_PORT);
-    v3_dev_unhook_io(dev, COM4_SCRATCH_PORT);
-
+static int serial_free(struct vm_device * dev) {
     return 0;
 }
 
@@ -883,11 +842,7 @@ static int serial_deinit(struct vm_device * dev) {
 
 
 static struct v3_device_ops dev_ops = {
-    //.init = serial_init,
-    .free = serial_deinit,
-    .reset = NULL,
-    .start = NULL,
-    .stop = NULL,
+    .free = serial_free,
 };
 
 
@@ -915,23 +870,83 @@ static int init_serial_port(struct serial_port * com) {
     com->rx_buffer.full = 0;
     memset(com->rx_buffer.buffer, 0, SERIAL_BUF_LEN);
     
+    com->ops = NULL;
+    com->backend_data = NULL;
+
+    return 0;
+}
+
+static int serial_input(struct v3_vm_info * vm, uint8_t * buf, uint64_t len, void * priv_data){
+    struct serial_port * com_port = (struct serial_port *)priv_data;
+    int i;
+
+    for(i = 0; i < len; i++){
+       queue_data(vm, com_port, &(com_port->rx_buffer), buf[i]);
+    }
+
+    return len;
+}
+
+
+static int connect_fn(struct v3_vm_info * vm, 
+                     void * frontend_data, 
+                     struct v3_dev_char_ops * ops, 
+                     v3_cfg_tree_t * cfg, 
+                     void * private_data, 
+                     void ** push_fn_arg) {
+
+    struct serial_state * serial = (struct serial_state *)frontend_data;
+    struct serial_port * com = NULL;
+    char * com_port = v3_cfg_val(cfg, "com_port");
+    int com_idx = 0;
+
+    if (com_port == NULL) {
+       PrintError("Invalid Serial frontend config: missing \"com_port\"\n");
+       return -1;
+    }
+    
+    com_idx = atoi(com_port) - 1;
+
+    if ((com_idx > 3) || (com_idx < 0)) {
+       PrintError("Invalid Com port (%s) \n", com_port);
+       return -1;
+    }
+
+    com = &(serial->coms[com_idx]);
+
+    com->ops = ops;
+    com->backend_data = private_data;
+
+    com->ops->push = serial_input;
+    *push_fn_arg = com;
+
     return 0;
 }
 
 static int serial_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
-    struct serial_state * state = (struct serial_state *)V3_Malloc(sizeof(struct serial_state));
+    struct serial_state * state = NULL;
     char * dev_id = v3_cfg_val(cfg, "ID");
 
-    PrintDebug("UART: init_device\n");
-    init_serial_port(&(state->com1));
-    init_serial_port(&(state->com2));
-    init_serial_port(&(state->com3));
-    init_serial_port(&(state->com4));
+    state = (struct serial_state *)V3_Malloc(sizeof(struct serial_state));
+    
+    if (state == NULL) {
+       PrintError("Could not allocate Serial Device\n");
+       return -1;
+    }
+    
+    memset(state, 0, sizeof(struct serial_state));
+
+
+
+    init_serial_port(&(state->coms[0]));
+    init_serial_port(&(state->coms[1]));
+    init_serial_port(&(state->coms[2]));
+    init_serial_port(&(state->coms[3]));
 
-    state->com1.irq_number = COM1_IRQ;
-    state->com2.irq_number = COM2_IRQ;
-    state->com3.irq_number = COM3_IRQ;
-    state->com4.irq_number = COM4_IRQ;
+    state->coms[0].irq_number = COM1_IRQ;
+    state->coms[1].irq_number = COM2_IRQ;
+    state->coms[2].irq_number = COM3_IRQ;
+    state->coms[3].irq_number = COM4_IRQ;
 
 
     struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, state);
@@ -941,6 +956,8 @@ static int serial_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
        return -1;
     }
 
+    PrintDebug("Serial device attached\n");
+
     v3_dev_hook_io(dev, COM1_DATA_PORT, &read_data_port, &write_data_port);
     v3_dev_hook_io(dev, COM1_IRQ_ENABLE_PORT, &read_ctrl_port, &write_ctrl_port);
     v3_dev_hook_io(dev, COM1_FIFO_CTRL_PORT, &read_ctrl_port, &write_ctrl_port);
@@ -977,8 +994,21 @@ static int serial_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
     v3_dev_hook_io(dev, COM4_MODEM_STATUS_PORT, &read_status_port, &write_status_port);
     v3_dev_hook_io(dev, COM4_SCRATCH_PORT, &read_ctrl_port, &write_ctrl_port);
 
+    PrintDebug("Serial ports hooked\n");
+
+
+
+    if (v3_dev_add_char_frontend(vm, dev_id, connect_fn, (void *)state) == -1) {
+       PrintError("Could not register %s as frontend\n", dev_id);
+       return -1;
+    }
+
+
     return 0;
 }
 
 
+
+
+
 device_register("SERIAL", serial_init)