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) 2010, Rumou Duan <duanrumou@gmail.com>
11 * Copyright (c) 2010, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Rumou Duan <duanrumou@gmail.com>
15 * Lei Xia <lxia@northwestern.edu>
17 * This is free software. You are permitted to use,
18 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
22 #include <palacios/vmm.h>
23 #include <palacios/vmm_dev_mgr.h>
24 #include <palacios/vmm_types.h>
26 #include <palacios/vmm_ringbuffer.h>
27 #include <palacios/vmm_lock.h>
28 #include <palacios/vmm_intr.h>
29 #include <palacios/vm_guest.h>
31 #include <devices/serial.h>
34 #ifndef CONFIG_DEBUG_SERIAL
36 #define PrintDebug(fmt, args...)
40 #define COM1_DATA_PORT 0x3f8
41 #define COM1_IRQ_ENABLE_PORT 0x3f9
42 #define COM1_DIV_LATCH_LSB_PORT 0x3f8
43 #define COM1_DIV_LATCH_MSB_PORT 0x3f9
44 #define COM1_IIR_PORT 0x3fa
45 #define COM1_FIFO_CTRL_PORT 0x3fa
46 #define COM1_LINE_CTRL_PORT 0x3fb
47 #define COM1_MODEM_CTRL_PORT 0x3fc
48 #define COM1_LINE_STATUS_PORT 0x3fd
49 #define COM1_MODEM_STATUS_PORT 0x3fe
50 #define COM1_SCRATCH_PORT 0x3ff
52 #define COM2_DATA_PORT 0x2f8
53 #define COM2_IRQ_ENABLE_PORT 0x2f9
54 #define COM2_DIV_LATCH_LSB_PORT 0x2f8
55 #define COM2_DIV_LATCH_MSB_PORT 0x2f9
56 #define COM2_IIR_PORT 0x2fa
57 #define COM2_FIFO_CTRL_PORT 0x2fa
58 #define COM2_LINE_CTRL_PORT 0x2fb
59 #define COM2_MODEM_CTRL_PORT 0x2fc
60 #define COM2_LINE_STATUS_PORT 0x2fd
61 #define COM2_MODEM_STATUS_PORT 0x2fe
62 #define COM2_SCRATCH_PORT 0x2ff
64 #define COM3_DATA_PORT 0x3e8
65 #define COM3_IRQ_ENABLE_PORT 0x3e9
66 #define COM3_DIV_LATCH_LSB_PORT 0x3e8
67 #define COM3_DIV_LATCH_MSB_PORT 0x3e9
68 #define COM3_IIR_PORT 0x3ea
69 #define COM3_FIFO_CTRL_PORT 0x3ea
70 #define COM3_LINE_CTRL_PORT 0x3eb
71 #define COM3_MODEM_CTRL_PORT 0x3ec
72 #define COM3_LINE_STATUS_PORT 0x3ed
73 #define COM3_MODEM_STATUS_PORT 0x3ee
74 #define COM3_SCRATCH_PORT 0x3ef
76 #define COM4_DATA_PORT 0x2e8
77 #define COM4_IRQ_ENABLE_PORT 0x2e9
78 #define COM4_DIV_LATCH_LSB_PORT 0x2e8
79 #define COM4_DIV_LATCH_MSB_PORT 0x2e9
80 #define COM4_IIR_PORT 0x2ea
81 #define COM4_FIFO_CTRL_PORT 0x2ea
82 #define COM4_LINE_CTRL_PORT 0x2eb
83 #define COM4_MODEM_CTRL_PORT 0x2ec
84 #define COM4_LINE_STATUS_PORT 0x2ed
85 #define COM4_MODEM_STATUS_PORT 0x2ee
86 #define COM4_SCRATCH_PORT 0x2ef
89 // Interrupt IDs (in priority order, highest is first)
90 #define STATUS_IRQ_LSR_OE_SET 0x3
91 #define STATUS_IRQ_LSR_PE_SET 0x3
92 #define STATUS_IRQ_LSR_FE_SET 0x3
93 #define STATUS_IRQ_LSR_BI_SET 0x3
95 #define RX_IRQ_TRIGGER_LEVEL 0x2
97 #define TX_IRQ_THRE 0x1
98 #define MODEL_IRQ_DELTA_SET 0x0
106 #define RX_BUFFER 0x1
107 #define TX_BUFFER 0x2
109 //initial value for registers
111 #define IER_INIT_VAL 0x3
112 //receive data available interrupt and THRE interrupt are enabled
113 #define IIR_INIT_VAL 0x1
114 //No Pending Interrupt bit is set.
115 #define FCR_INIT_VAL 0xc0
116 //fifo control register is set to 0
117 #define LCR_INIT_VAL 0x3
118 #define MCR_INIT_VAL 0x0
119 #define LSR_INIT_VAL 0x60
120 #define MSR_INIT_VAL 0x0
121 #define DLL_INIT_VAL 0x1
122 #define DLM_INIT_VAL 0x0
126 //receiver buffer register
127 struct rbr_register {
131 // transmitter holding register
132 struct thr_register {
136 //interrupt enable register
137 struct ier_register {
141 uint8_t erbfi : 1; // Enable Receiver Buffer full interrupt
142 uint8_t etbei : 1; // Enable Transmit buffer empty interrupt
143 uint8_t elsi : 1; // Enable Line Status Interrupt
144 uint8_t edssi : 1; // Enable Delta Status signals interrupt
145 uint8_t rsvd : 4; // MBZ
146 } __attribute__((packed));
147 } __attribute__((packed));
148 } __attribute__((packed));
151 //interrupt identification register
152 struct iir_register {
156 uint8_t pending : 1; // Interrupt pending (0=interrupt pending)
157 uint8_t iid : 3; // Interrupt Identification
158 uint8_t rsvd : 2; // MBZ
159 uint8_t fifo_en : 2; // FIFO enable
160 } __attribute__((packed));
161 } __attribute__((packed));
162 } __attribute__((packed));
164 //FIFO control register
165 struct fcr_register {
169 uint8_t enable : 1; // enable fifo
170 uint8_t rfres : 1; // RX FIFO reset
171 uint8_t xfres : 1; // TX FIFO reset
172 uint8_t dma_sel : 1; // DMA mode select
173 uint8_t rsvd : 2; // MBZ
174 uint8_t rx_trigger: 2; // RX FIFO trigger level select
175 } __attribute__((packed));
176 } __attribute__((packed));
177 } __attribute__((packed));
180 //line control register
181 struct lcr_register {
185 uint8_t word_len : 2; // word length select
186 uint8_t stop_bits : 1; // Stop Bit select
187 uint8_t parity_enable : 1; // Enable parity
188 uint8_t even_sel : 1; // Even Parity Select
189 uint8_t stick_parity : 1; // Stick Parity Select
190 uint8_t sbr : 1; // Set Break
191 uint8_t dlab : 1; // Divisor latch access bit
192 } __attribute__((packed));
193 } __attribute__((packed));
194 } __attribute__((packed));
197 //modem control register
198 struct mcr_register {
206 uint8_t loop : 1; // loopback mode
207 uint8_t rsvd : 3; // MBZ
208 } __attribute__((packed));
209 } __attribute__((packed));
210 } __attribute__((packed));
213 //line status register
214 struct lsr_register {
218 uint8_t dr : 1; // data ready
219 uint8_t oe : 1; // Overrun error
220 uint8_t pe : 1; // Parity Error
221 uint8_t fe : 1; // Framing Error
222 uint8_t brk : 1; // broken line detected
223 uint8_t thre : 1; // Transmitter holding register empty
224 uint8_t temt : 1; // Transmitter Empty
225 uint8_t fifo_err : 1; // at least one error is pending in the RX FIFO chain
226 } __attribute__((packed));
227 } __attribute__((packed));
228 } __attribute__((packed));
231 struct msr_register {
235 uint8_t dcts : 1; // Delta Clear To Send
236 uint8_t ddsr : 1; // Delta Data Set Ready
237 uint8_t teri : 1; // Trailing Edge Ring Indicator
238 uint8_t ddcd : 1; // Delta Data Carrier Detect
239 uint8_t cts : 1; // Clear to Send
240 uint8_t dsr : 1; // Data Set Ready
241 uint8_t ri : 1; // Ring Indicator
242 uint8_t dcd : 1; // Data Carrier Detect
243 } __attribute__((packed));
244 } __attribute__((packed));
245 } __attribute__((packed));
248 struct scr_register {
253 struct dll_register {
258 struct dlm_register {
261 #define SERIAL_BUF_LEN 16
263 struct serial_buffer {
264 int head; // most recent data
265 int tail; // oldest char
267 uint8_t buffer[SERIAL_BUF_LEN];
271 struct rbr_register rbr;
272 struct thr_register thr;
273 struct ier_register ier;
274 struct iir_register iir;
275 struct fcr_register fcr;
276 struct lcr_register lcr;
277 struct mcr_register mcr;
278 struct lsr_register lsr;
279 struct msr_register msr;
280 struct scr_register scr;
281 struct dll_register dll;
282 struct dlm_register dlm;
285 struct serial_buffer tx_buffer;
286 struct serial_buffer rx_buffer;
291 struct v3_dev_char_ops * ops;
296 struct serial_state {
297 struct serial_port coms[4];
303 static struct serial_port * get_com_from_port(struct serial_state * serial, uint16_t port) {
304 if ((port >= COM1_DATA_PORT) && (port <= COM1_SCRATCH_PORT)) {
305 return &(serial->coms[0]);
306 } else if ((port >= COM2_DATA_PORT) && (port <= COM2_SCRATCH_PORT)) {
307 return &(serial->coms[1]);
308 } else if ((port >= COM3_DATA_PORT) && (port <= COM3_SCRATCH_PORT)) {
309 return &(serial->coms[2]);
310 } else if ((port >= COM4_DATA_PORT) && (port <= COM4_SCRATCH_PORT)) {
311 return &(serial->coms[3]);
313 PrintError("Error: Could not find serial port associated with IO port %d\n", port);
318 static inline bool receive_buffer_trigger(int number, int trigger_number) {
320 switch (trigger_number) {
322 return (number >= 1);
324 return (number >= 4);
326 return (number >= 8);
328 return (number >= 14);
334 static int getNumber(struct serial_buffer * buf) {
335 int number = buf->head - buf->tail;
337 if (buf->full == 1) {
338 return SERIAL_BUF_LEN;
339 } else if (number >= 0) {
342 return SERIAL_BUF_LEN + number;
346 static int updateIRQ(struct v3_vm_info * vm, struct serial_port * com) {
348 if ( (com->ier.erbfi == 0x1) &&
349 (receive_buffer_trigger( getNumber(&(com->rx_buffer)), com->fcr.rx_trigger)) ) {
351 PrintDebug("UART: receive buffer interrupt(trigger level reached)");
353 com->iir.iid = RX_IRQ_TRIGGER_LEVEL;
354 v3_raise_irq(vm, com->irq_number);
357 if ( (com->iir.iid == RX_IRQ_TRIGGER_LEVEL) &&
358 (!(receive_buffer_trigger( getNumber(&(com->rx_buffer)), com->fcr.rx_trigger))) ) {
360 com->iir.iid = 0x0; //reset interrupt identification register
361 com->iir.pending = 0x1;
364 if ( (com->iir.iid == TX_IRQ_THRE) &&
365 (getNumber(&(com->tx_buffer)) == SERIAL_BUF_LEN)) {
367 com->iir.iid = 0x0; //reset interrupt identification register
368 com->iir.pending = 0x1;
370 } else if ( (com->ier.etbei == 0x1) &&
371 (getNumber(&(com->tx_buffer)) != SERIAL_BUF_LEN )) {
373 PrintDebug("UART: transmit buffer interrupt(buffer not full)");
375 com->iir.iid = TX_IRQ_THRE;
376 com->iir.pending = 0;
378 v3_raise_irq(vm, com->irq_number);
385 static int queue_data(struct v3_vm_info * vm, struct serial_port * com,
386 struct serial_buffer * buf, uint8_t data) {
387 int next_loc = (buf->head + 1) % SERIAL_BUF_LEN;
389 if (buf->full == 1) {
390 PrintDebug("Buffer is full!\n");
392 if (buf == &(com->rx_buffer)) {
393 com->lsr.oe = 1; //overrun error bit set
399 buf->buffer[next_loc] = data;
400 buf->head = next_loc;
402 if (buf->head == buf->tail) {
406 if (buf == &(com->rx_buffer)) {
407 com->lsr.dr = 1; //as soon as new data arrives at receive buffer, set data ready bit in lsr.
410 if (buf == &(com->tx_buffer)) {
411 com->lsr.thre = 0; //reset thre and temt bits.
420 static int dequeue_data(struct v3_vm_info * vm, struct serial_port * com,
421 struct serial_buffer * buf, uint8_t * data) {
423 int next_tail = (buf->tail + 1) % SERIAL_BUF_LEN;
426 if ( (buf->head == buf->tail) && (buf->full != 1) ) {
427 PrintDebug("no data to delete!\n");
431 if (buf->full == 1) {
436 *data = buf->buffer[next_tail];
437 buf->buffer[next_tail] = 0;
438 buf->tail = next_tail;
440 if ( (buf == &(com->rx_buffer)) && (getNumber(&(com->rx_buffer)) == 0) ) {
444 if ((buf == &(com->tx_buffer)) && (getNumber(&(com->tx_buffer)) == 0)) {
454 static int write_data_port(struct guest_info * core, uint16_t port,
455 void * src, uint_t length, void * priv_data) {
456 struct serial_state * state = priv_data;
457 uint8_t * val = (uint8_t *)src;
458 struct serial_port * com_port = NULL;
460 PrintDebug("Write to Data Port 0x%x (val=%x)\n", port, *val);
463 PrintDebug("Invalid length(%d) in write to 0x%x\n", length, port);
467 if ((port != COM1_DATA_PORT) && (port != COM2_DATA_PORT) &&
468 (port != COM3_DATA_PORT) && (port != COM4_DATA_PORT)) {
469 PrintError("Serial Read data port for illegal port Number (%d)\n", port);
473 com_port = get_com_from_port(state, port);
475 if (com_port == NULL) {
476 PrintDebug("UART:read from NOBODY");
481 // dlab is always checked first
482 if (com_port->lcr.dlab == 1) {
483 com_port->dll.data = *val;
487 /* JRL: Some buffering would probably be a good idea here.... */
489 com_port->ops->write(val, 1, com_port->backend_data);
491 queue_data(core->vm_info, com_port, &(com_port->tx_buffer), *val);
500 static int read_data_port(struct guest_info * core, uint16_t port,
501 void * dst, uint_t length, void * priv_data) {
502 struct serial_state * state = priv_data;
503 uint8_t * val = (uint8_t *)dst;
504 struct serial_port * com_port = NULL;
506 PrintDebug("Read from Data Port 0x%x\n", port);
509 PrintDebug("Invalid length(%d) in write to 0x%x\n", length, port);
513 if ((port != COM1_DATA_PORT) && (port != COM2_DATA_PORT) &&
514 (port != COM3_DATA_PORT) && (port != COM4_DATA_PORT)) {
515 PrintError("Serial Read data port for illegal port Number (%d)\n", port);
519 com_port = get_com_from_port(state, port);
521 if (com_port == NULL) {
522 PrintDebug("UART:read from NOBODY");
526 if (com_port->lcr.dlab == 1) {
527 *val = com_port->dll.data;
529 dequeue_data(core->vm_info, com_port, &(com_port->rx_buffer), val);
537 static int handle_fcr_write(struct serial_port * com, uint8_t value) {
539 com->fcr.enable = value & 0x1;
541 if (com->fcr.enable == 0x1) {
542 com->fcr.val = value;
544 com->fcr.enable = 1; // Do we need to set this??
546 //if rfres set, clear receive buffer.
547 if (com->fcr.rfres == 0x1) {
548 com->rx_buffer.head = 0;
549 com->rx_buffer.tail = 0;
550 com->rx_buffer.full = 0;
551 memset(com->rx_buffer.buffer, 0, SERIAL_BUF_LEN);
555 //if xfres set, clear transmit buffer.
556 if (com->fcr.xfres == 0x1) {
557 com->tx_buffer.head = 0;
558 com->tx_buffer.tail = 0;
559 com->tx_buffer.full = 0;
560 memset(com->tx_buffer.buffer, 0, SERIAL_BUF_LEN);
564 //clear both buffers.
565 com->tx_buffer.head = 0;
566 com->tx_buffer.tail = 0;
567 com->tx_buffer.full = 0;
568 com->rx_buffer.head = 0;
569 com->rx_buffer.tail = 0;
570 com->rx_buffer.full = 0;
572 memset(com->rx_buffer.buffer, 0, SERIAL_BUF_LEN);
573 memset(com->tx_buffer.buffer, 0, SERIAL_BUF_LEN);
583 static int write_ctrl_port(struct guest_info * core, uint16_t port, void * src,
584 uint_t length, void * priv_data) {
585 struct serial_state * state = priv_data;
586 uint8_t val = *(uint8_t *)src;
587 struct serial_port * com_port = NULL;
589 PrintDebug("UART:Write to Control Port (val=%x)\n", val);
592 PrintDebug("UART:Invalid Write length to control port%d\n", port);
596 com_port = get_com_from_port(state, port);
598 if (com_port == NULL) {
599 PrintError("Could not find serial port corresponding to IO port %d\n", port);
603 //always check dlab first
605 case COM1_IRQ_ENABLE_PORT:
606 case COM2_IRQ_ENABLE_PORT:
607 case COM3_IRQ_ENABLE_PORT:
608 case COM4_IRQ_ENABLE_PORT: {
609 PrintDebug("UART:Write to IER/LATCH port: dlab is %x\n", com_port->lcr.dlab);
611 if (com_port->lcr.dlab == 1) {
612 com_port->dlm.data = val;
614 com_port->ier.val = val;
619 case COM1_FIFO_CTRL_PORT:
620 case COM2_FIFO_CTRL_PORT:
621 case COM3_FIFO_CTRL_PORT:
622 case COM4_FIFO_CTRL_PORT: {
623 PrintDebug("UART:Write to FCR");
625 if (handle_fcr_write(com_port, val) == -1) {
631 case COM1_LINE_CTRL_PORT:
632 case COM2_LINE_CTRL_PORT:
633 case COM3_LINE_CTRL_PORT:
634 case COM4_LINE_CTRL_PORT: {
635 PrintDebug("UART:Write to LCR");
636 com_port->lcr.val = val;
639 case COM1_MODEM_CTRL_PORT:
640 case COM2_MODEM_CTRL_PORT:
641 case COM3_MODEM_CTRL_PORT:
642 case COM4_MODEM_CTRL_PORT: {
643 PrintDebug("UART:Write to MCR");
644 com_port->mcr.val = val;
647 case COM1_SCRATCH_PORT:
648 case COM2_SCRATCH_PORT:
649 case COM3_SCRATCH_PORT:
650 case COM4_SCRATCH_PORT: {
651 PrintDebug("UART:Write to SCRATCH");
652 com_port->scr.data = val;
656 PrintDebug("UART:Write to NOBODY, ERROR");
667 static int read_ctrl_port(struct guest_info * core, uint16_t port, void * dst,
668 uint_t length, void * priv_data) {
669 struct serial_state * state = priv_data;
670 uint8_t * val = (uint8_t *)dst;
671 struct serial_port * com_port = NULL;
673 PrintDebug("Read from Control Port\n");
676 PrintDebug("Invalid Read length to control port\n");
680 com_port = get_com_from_port(state, port);
682 if (com_port == NULL) {
683 PrintError("Could not find serial port corresponding to IO port %d\n", port);
687 //always check dlab first
689 case COM1_IRQ_ENABLE_PORT:
690 case COM2_IRQ_ENABLE_PORT:
691 case COM3_IRQ_ENABLE_PORT:
692 case COM4_IRQ_ENABLE_PORT: {
693 PrintDebug("UART:read from IER");
695 if (com_port->lcr.dlab == 1) {
696 *val = com_port->dlm.data;
698 *val = com_port->ier.val;
703 case COM1_FIFO_CTRL_PORT:
704 case COM2_FIFO_CTRL_PORT:
705 case COM3_FIFO_CTRL_PORT:
706 case COM4_FIFO_CTRL_PORT:
707 PrintDebug("UART:read from FCR");
708 *val = com_port->fcr.val;
711 case COM1_LINE_CTRL_PORT:
712 case COM2_LINE_CTRL_PORT:
713 case COM3_LINE_CTRL_PORT:
714 case COM4_LINE_CTRL_PORT:
715 PrintDebug("UART:read from LCR");
716 *val = com_port->lcr.val;
719 case COM1_MODEM_CTRL_PORT:
720 case COM2_MODEM_CTRL_PORT:
721 case COM3_MODEM_CTRL_PORT:
722 case COM4_MODEM_CTRL_PORT:
723 PrintDebug("UART:read from MCR");
724 *val = com_port->mcr.val;
727 case COM1_SCRATCH_PORT:
728 case COM2_SCRATCH_PORT:
729 case COM3_SCRATCH_PORT:
730 case COM4_SCRATCH_PORT:
731 PrintDebug("UART:read from SCRATCH");
732 *val = com_port->scr.data;
736 PrintDebug("UART:read from NOBODY");
744 static int write_status_port(struct guest_info * core, uint16_t port, void * src,
745 uint_t length, void * priv_data) {
746 struct serial_state * state = priv_data;
747 uint8_t val = *(uint8_t *)src;
748 struct serial_port * com_port = NULL;
750 PrintDebug("Write to Status Port (val=%x)\n", val);
753 PrintDebug("Invalid Write length to status port %d\n", port);
757 com_port = get_com_from_port(state, port);
759 if (com_port == NULL) {
760 PrintError("Could not find serial port corresponding to IO port %d\n", port);
765 case COM1_LINE_STATUS_PORT:
766 case COM2_LINE_STATUS_PORT:
767 case COM3_LINE_STATUS_PORT:
768 case COM4_LINE_STATUS_PORT:
769 PrintDebug("UART:write to LSR");
770 com_port->lsr.val = val;
773 case COM1_MODEM_STATUS_PORT:
774 case COM2_MODEM_STATUS_PORT:
775 case COM3_MODEM_STATUS_PORT:
776 case COM4_MODEM_STATUS_PORT:
777 PrintDebug("UART:write to MSR");
778 com_port->msr.val = val;
782 PrintDebug("UART:write to NOBODY");
789 static int read_status_port(struct guest_info * core, uint16_t port, void * dst,
790 uint_t length, void * priv_data) {
791 struct serial_state * state = priv_data;
792 uint8_t * val = (uint8_t *)dst;
793 struct serial_port * com_port = NULL;
795 PrintDebug("Read from Status Port 0x%x\n", port);
798 PrintDebug("Invalid Read length to control port\n");
802 com_port = get_com_from_port(state, port);
804 if (com_port == NULL) {
805 PrintError("Could not find serial port corresponding to IO port %d\n", port);
810 case COM1_LINE_STATUS_PORT:
811 case COM2_LINE_STATUS_PORT:
812 case COM3_LINE_STATUS_PORT:
813 case COM4_LINE_STATUS_PORT:
814 PrintDebug("UART:read from LSR");
816 *val = com_port->lsr.val;
817 com_port->lsr.oe = 0; // Why do we clear this??
821 case COM1_MODEM_STATUS_PORT:
822 case COM2_MODEM_STATUS_PORT:
823 case COM3_MODEM_STATUS_PORT:
824 case COM4_MODEM_STATUS_PORT:
825 PrintDebug("UART:read from COM4 MSR");
826 *val = com_port->msr.val;
830 PrintDebug("UART:read from NOBODY");
837 static int serial_deinit(struct vm_device * dev) {
844 static struct v3_device_ops dev_ops = {
845 //.init = serial_init,
846 .free = serial_deinit,
852 static int init_serial_port(struct serial_port * com) {
854 com->ier.val = IER_INIT_VAL;
855 com->iir.val = IIR_INIT_VAL;
856 com->fcr.val = FCR_INIT_VAL;
857 com->lcr.val = LCR_INIT_VAL;
858 com->mcr.val = MCR_INIT_VAL;
859 com->lsr.val = LSR_INIT_VAL;
860 com->msr.val = MSR_INIT_VAL;
862 com->dll.data = DLL_INIT_VAL;
863 com->dlm.data = DLM_INIT_VAL;
865 com->tx_buffer.head = 0;
866 com->tx_buffer.tail = 0;
867 com->tx_buffer.full = 0;
868 memset(com->tx_buffer.buffer, 0, SERIAL_BUF_LEN);
870 com->rx_buffer.head = 0;
871 com->rx_buffer.tail = 0;
872 com->rx_buffer.full = 0;
873 memset(com->rx_buffer.buffer, 0, SERIAL_BUF_LEN);
876 com->backend_data = NULL;
881 static int serial_input(struct v3_vm_info * vm, uint8_t * buf, uint64_t len, void * priv_data){
882 struct serial_port * com_port = (struct serial_port *)priv_data;
885 for(i = 0; i < len; i++){
886 queue_data(vm, com_port, &(com_port->rx_buffer), buf[i]);
893 static int connect_fn(struct v3_vm_info * vm,
894 void * frontend_data,
895 struct v3_dev_char_ops * ops,
898 void ** push_fn_arg) {
900 struct serial_state * serial = (struct serial_state *)frontend_data;
901 struct serial_port * com = NULL;
902 char * com_port = v3_cfg_val(cfg, "com_port");
905 if (com_port == NULL) {
906 PrintError("Invalid Serial frontend config: missing \"com_port\"\n");
910 com_idx = atoi(com_port) - 1;
912 if ((com_idx > 3) || (com_idx < 0)) {
913 PrintError("Invalid Com port (%s) \n", com_port);
917 com = &(serial->coms[com_idx]);
920 com->backend_data = private_data;
922 com->ops->push = serial_input;
928 static int serial_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
929 struct serial_state * state = NULL;
930 char * dev_id = v3_cfg_val(cfg, "ID");
932 state = (struct serial_state *)V3_Malloc(sizeof(struct serial_state));
935 PrintError("Could not allocate Serial Device\n");
939 memset(state, 0, sizeof(struct serial_state));
943 init_serial_port(&(state->coms[0]));
944 init_serial_port(&(state->coms[1]));
945 init_serial_port(&(state->coms[2]));
946 init_serial_port(&(state->coms[3]));
948 state->coms[0].irq_number = COM1_IRQ;
949 state->coms[1].irq_number = COM2_IRQ;
950 state->coms[2].irq_number = COM3_IRQ;
951 state->coms[3].irq_number = COM4_IRQ;
954 struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, state);
956 if (v3_attach_device(vm, dev) == -1) {
957 PrintError("Could not attach device %s\n", dev_id);
961 PrintDebug("Serial device attached\n");
963 v3_dev_hook_io(dev, COM1_DATA_PORT, &read_data_port, &write_data_port);
964 v3_dev_hook_io(dev, COM1_IRQ_ENABLE_PORT, &read_ctrl_port, &write_ctrl_port);
965 v3_dev_hook_io(dev, COM1_FIFO_CTRL_PORT, &read_ctrl_port, &write_ctrl_port);
966 v3_dev_hook_io(dev, COM1_LINE_CTRL_PORT, &read_ctrl_port, &write_ctrl_port);
967 v3_dev_hook_io(dev, COM1_MODEM_CTRL_PORT, &read_ctrl_port, &write_ctrl_port);
968 v3_dev_hook_io(dev, COM1_LINE_STATUS_PORT, &read_status_port, &write_status_port);
969 v3_dev_hook_io(dev, COM1_MODEM_STATUS_PORT, &read_status_port, &write_status_port);
970 v3_dev_hook_io(dev, COM1_SCRATCH_PORT, &read_ctrl_port, &write_ctrl_port);
972 v3_dev_hook_io(dev, COM2_DATA_PORT, &read_data_port, &write_data_port);
973 v3_dev_hook_io(dev, COM2_IRQ_ENABLE_PORT, &read_ctrl_port, &write_ctrl_port);
974 v3_dev_hook_io(dev, COM2_FIFO_CTRL_PORT, &read_ctrl_port, &write_ctrl_port);
975 v3_dev_hook_io(dev, COM2_LINE_CTRL_PORT, &read_ctrl_port, &write_ctrl_port);
976 v3_dev_hook_io(dev, COM2_MODEM_CTRL_PORT, &read_ctrl_port, &write_ctrl_port);
977 v3_dev_hook_io(dev, COM2_LINE_STATUS_PORT, &read_status_port, &write_status_port);
978 v3_dev_hook_io(dev, COM2_MODEM_STATUS_PORT, &read_status_port, &write_status_port);
979 v3_dev_hook_io(dev, COM2_SCRATCH_PORT, &read_ctrl_port, &write_ctrl_port);
981 v3_dev_hook_io(dev, COM3_DATA_PORT, &read_data_port, &write_data_port);
982 v3_dev_hook_io(dev, COM3_IRQ_ENABLE_PORT, &read_ctrl_port, &write_ctrl_port);
983 v3_dev_hook_io(dev, COM3_FIFO_CTRL_PORT, &read_ctrl_port, &write_ctrl_port);
984 v3_dev_hook_io(dev, COM3_LINE_CTRL_PORT, &read_ctrl_port, &write_ctrl_port);
985 v3_dev_hook_io(dev, COM3_MODEM_CTRL_PORT, &read_ctrl_port, &write_ctrl_port);
986 v3_dev_hook_io(dev, COM3_LINE_STATUS_PORT, &read_status_port, &write_status_port);
987 v3_dev_hook_io(dev, COM3_MODEM_STATUS_PORT, &read_status_port, &write_status_port);
988 v3_dev_hook_io(dev, COM3_SCRATCH_PORT, &read_ctrl_port, &write_ctrl_port);
990 v3_dev_hook_io(dev, COM4_DATA_PORT, &read_data_port, &write_data_port);
991 v3_dev_hook_io(dev, COM4_IRQ_ENABLE_PORT, &read_ctrl_port, &write_ctrl_port);
992 v3_dev_hook_io(dev, COM4_FIFO_CTRL_PORT, &read_ctrl_port, &write_ctrl_port);
993 v3_dev_hook_io(dev, COM4_LINE_CTRL_PORT, &read_ctrl_port, &write_ctrl_port);
994 v3_dev_hook_io(dev, COM4_MODEM_CTRL_PORT, &read_ctrl_port, &write_ctrl_port);
995 v3_dev_hook_io(dev, COM4_LINE_STATUS_PORT, &read_status_port, &write_status_port);
996 v3_dev_hook_io(dev, COM4_MODEM_STATUS_PORT, &read_status_port, &write_status_port);
997 v3_dev_hook_io(dev, COM4_SCRATCH_PORT, &read_ctrl_port, &write_ctrl_port);
999 PrintDebug("Serial ports hooked\n");
1003 if (v3_dev_add_char_frontend(vm, dev_id, connect_fn, (void *)state) == -1) {
1004 PrintError("Could not register %s as frontend\n", dev_id);
1016 device_register("SERIAL", serial_init)