xref: /kvmtool/hw/serial.c (revision a93ec68b668539efd4077bed893bdf10ab95f4a0)
1 #include "kvm/early_printk.h"
2 
3 #include "kvm/ioport.h"
4 
5 #include <stdio.h>
6 
7 static int early_serial_base = 0x3f8;  /* ttyS0 */
8 
9 #define XMTRDY          0x20
10 
11 #define TXR             0       /*  Transmit register (WRITE) */
12 #define LSR             5       /*  Line Status               */
13 #define MSR		6	/*  Modem Status              */
14 
15 static bool early_serial_txr_out(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
16 {
17 	char *p = data;
18 	int i;
19 
20 	while (count--) {
21 		for (i = 0; i < size; i++)
22 			fprintf(stderr, "%c", *p++);
23 	}
24 	fflush(stderr);
25 
26 	return true;
27 }
28 
29 static bool early_serial_rxr_in(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
30 {
31 	return true;
32 }
33 
34 static struct ioport_operations early_serial_txr_rxr_ops = {
35 	.io_out		= early_serial_txr_out,
36 	.io_in		= early_serial_rxr_in,
37 };
38 
39 static bool early_serial_lsr_in(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
40 {
41 	uint8_t *p = data;
42 
43 	*p	= XMTRDY;
44 
45 	return true;
46 }
47 
48 static struct ioport_operations early_serial_lsr_ops = {
49 	.io_in		= early_serial_lsr_in,
50 };
51 
52 static bool early_serial_msr_in(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
53 {
54 	return true;
55 }
56 
57 static struct ioport_operations early_serial_msr_ops = {
58 	.io_in		= early_serial_msr_in,
59 };
60 
61 void early_printk__init(void)
62 {
63 	ioport__register(early_serial_base + TXR, &early_serial_txr_rxr_ops, 1);
64 	ioport__register(early_serial_base + LSR, &early_serial_lsr_ops, 1);
65 	ioport__register(early_serial_base + MSR, &early_serial_msr_ops, 1);
66 }
67