xref: /kvmtool/hw/serial.c (revision 0bc4cf78724592b5139b514b189d8bdb3bc9adf5)
1 #include "kvm/8250-serial.h"
2 
3 #include "kvm/ioport.h"
4 #include "kvm/util.h"
5 #include "kvm/kvm.h"
6 
7 #include <linux/serial_reg.h>
8 
9 #include <stdbool.h>
10 #include <poll.h>
11 
12 struct serial8250_device {
13 	uint16_t		iobase;
14 	uint8_t			irq;
15 
16 	uint8_t			thr;
17 	uint8_t			dll;
18 	uint8_t			dlm;
19 	uint8_t			iir;
20 	uint8_t			ier;
21 	uint8_t			fcr;
22 	uint8_t			lcr;
23 	uint8_t			mcr;
24 	uint8_t			lsr;
25 	uint8_t			scr;
26 
27 	uint8_t			counter;
28 };
29 
30 static struct serial8250_device devices[] = {
31 	/* ttyS0 */
32 	[0]	= {
33 		.iobase			= 0x3f8,
34 		.irq			= 4,
35 
36 		.lsr			= UART_LSR_TEMT | UART_LSR_THRE,
37 	},
38 	/* ttyS1 */
39 	[1]	= {
40 		.iobase			= 0x2f8,
41 		.irq			= 3,
42 
43 		.iir			= UART_IIR_NO_INT,
44 	},
45 	/* ttyS2 */
46 	[2]	= {
47 		.iobase			= 0x3e8,
48 		.irq			= 4,
49 
50 		.iir			= UART_IIR_NO_INT,
51 	},
52 };
53 
54 static int read_char(int fd)
55 {
56 	int c;
57 
58 	if (read(fd, &c, 1) < 0)
59 		return -1;
60 
61 	return c;
62 }
63 
64 static bool is_readable(int fd)
65 {
66 	struct pollfd pollfd = (struct pollfd) {
67 		.fd	= fd,
68 		.events	= POLLIN,
69 	};
70 
71 	return poll(&pollfd, 1, 0) > 0;
72 }
73 
74 /*
75  * Interrupts are injected for ttyS0 only.
76  */
77 void serial8250__interrupt(struct kvm *self)
78 {
79 	struct serial8250_device *dev = &devices[0];
80 	uint8_t new_iir;
81 
82 	dev->iir	= UART_IIR_NO_INT;
83 
84 	/* No interrupts enabled. Exit... */
85 	if (!(dev->ier & (UART_IER_THRI|UART_IER_RDI)))
86 		return;
87 
88 	new_iir		= 0;
89 
90 	/* We're always good for guest sending data. */
91 	if (dev->ier & UART_IER_THRI)
92 		new_iir			|= UART_IIR_THRI;
93 
94 	/* Is there input in stdin to send to the guest? */
95 	if (!(dev->lsr & UART_LSR_DR) && is_readable(fileno(stdin))) {
96 		int c;
97 
98 		c			= read_char(fileno(stdin));
99 		if (c >= 0) {
100 			dev->thr		= c;
101 			dev->lsr		|= UART_LSR_DR;
102 			new_iir			|= UART_IIR_RDI;
103 		}
104 	}
105 
106 	/* Only send an IRQ if there's work to do. */
107 	if (new_iir) {
108 		dev->counter		= 0;
109 		dev->iir		= new_iir;
110 		kvm__irq_line(self, dev->irq, 0);
111 		kvm__irq_line(self, dev->irq, 1);
112 	}
113 }
114 
115 static struct serial8250_device *find_device(uint16_t port)
116 {
117 	unsigned int i;
118 
119 	for (i = 0; i < ARRAY_SIZE(devices); i++) {
120 		struct serial8250_device *dev = &devices[i];
121 
122 		if (dev->iobase == (port & ~0x7))
123 			return dev;
124 	}
125 	return NULL;
126 }
127 
128 static bool serial8250_out(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
129 {
130 	struct serial8250_device *dev;
131 	uint16_t offset;
132 
133 	dev		= find_device(port);
134 	if (!dev)
135 		return false;
136 
137 	offset		= port - dev->iobase;
138 
139 	if (dev->lcr & UART_LCR_DLAB) {
140 		switch (offset) {
141 		case UART_DLL:
142 			dev->dll		= ioport__read8(data);
143 			break;
144 		case UART_DLM:
145 			dev->dlm		= ioport__read8(data);
146 			break;
147 		case UART_FCR:
148 			dev->fcr		= ioport__read8(data);
149 			break;
150 		case UART_LCR:
151 			dev->lcr		= ioport__read8(data);
152 			break;
153 		default:
154 			return false;
155 		}
156 	} else {
157 		switch (offset) {
158 		case UART_TX: {
159 			char *p = data;
160 			int i;
161 
162 			while (count--) {
163 				for (i = 0; i < size; i++)
164 					fprintf(stdout, "%c", *p++);
165 			}
166 			fflush(stdout);
167 
168 			if (dev->counter++ > 10)
169 				dev->iir		= UART_IIR_NO_INT;
170 
171 			break;
172 		}
173 		case UART_IER:
174 			dev->ier		= ioport__read8(data);
175 			break;
176 		case UART_FCR:
177 			dev->fcr		= ioport__read8(data);
178 			break;
179 		case UART_LCR:
180 			dev->lcr		= ioport__read8(data);
181 			break;
182 		case UART_MCR:
183 			dev->mcr		= ioport__read8(data);
184 			break;
185 		case UART_SCR:
186 			dev->scr		= ioport__read8(data);
187 			break;
188 		default:
189 			return false;
190 		}
191 	}
192 
193 	return true;
194 }
195 
196 static bool serial8250_in(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
197 {
198 	struct serial8250_device *dev;
199 	uint16_t offset;
200 
201 	dev		= find_device(port);
202 	if (!dev)
203 		return false;
204 
205 	offset		= port - dev->iobase;
206 
207 	if (dev->lcr & UART_LCR_DLAB)
208 		return false;
209 
210 	switch (offset) {
211 	case UART_TX:
212 		if (dev->lsr & UART_LSR_DR) {
213 			dev->iir		= UART_IIR_NO_INT;
214 
215 			dev->lsr		&= ~UART_LSR_DR;
216 			ioport__write8(data, dev->thr);
217 		}
218 		break;
219 	case UART_IER:
220 		ioport__write8(data, dev->ier);
221 		break;
222 	case UART_IIR:
223 		ioport__write8(data, dev->iir);
224 		break;
225 	case UART_LCR:
226 		ioport__write8(data, dev->lcr);
227 		break;
228 	case UART_MCR:
229 		ioport__write8(data, dev->mcr);
230 		break;
231 	case UART_LSR:
232 		ioport__write8(data, dev->lsr);
233 		break;
234 	case UART_MSR:
235 		ioport__write8(data, UART_MSR_CTS);
236 		break;
237 	case UART_SCR:
238 		ioport__write8(data, dev->scr);
239 		break;
240 	default:
241 		return false;
242 	}
243 
244 	return true;
245 }
246 
247 static struct ioport_operations serial8250_ops = {
248 	.io_in		= serial8250_in,
249 	.io_out		= serial8250_out,
250 };
251 
252 void serial8250__init(void)
253 {
254 	unsigned int i;
255 
256 	for (i = 0; i < ARRAY_SIZE(devices); i++) {
257 		struct serial8250_device *dev = &devices[i];
258 
259 		ioport__register(dev->iobase, &serial8250_ops, 8);
260 	}
261 }
262