xref: /dragonfly/test/nvmm/demo/toyvirt/toydev.c (revision 31815141)
1 /*
2  * Copyright (c) 2018-2021 Maxime Villard, m00nbsd.net
3  * All rights reserved.
4  *
5  * This code is part of the NVMM hypervisor.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * This file implements two really simple devices. One is an MMIO device, the
31  * other is an IO device.
32  */
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <stdbool.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <fcntl.h>
41 #include <err.h>
42 #include <sys/types.h>
43 #include <sys/ioctl.h>
44 #include <nvmm.h>
45 
46 #include "common.h"
47 
48 /* -------------------------------------------------------------------------- */
49 
50 /*
51  * LAPIC device, MMIO. We only handle the ID.
52  */
53 static void
54 toydev_lapic(gpaddr_t gpa, bool write, uint8_t *buf, size_t size)
55 {
56 	uint32_t *data;
57 
58 #define LAPIC_BASE		0xfee00000
59 #define	LAPIC_ID		0x020
60 	if (write) {
61 		printf("[!] Unexpected LAPIC write!\n");
62 		exit(EXIT_FAILURE);
63 	}
64 	if (size != sizeof(uint32_t)) {
65 		printf("[!] Unexpected LAPIC read size %zu!\n", size);
66 		exit(EXIT_FAILURE);
67 	}
68 	if (gpa == LAPIC_BASE + LAPIC_ID) {
69 		data = (uint32_t *)&buf[3];
70 		*data = 120;
71 	}
72 }
73 
74 /*
75  * Console device, IO. It retrieves a string on port 123, and we display it.
76  */
77 static int
78 toydev_cons(int port __unused, bool in, uint8_t *buf, size_t size)
79 {
80 	static bool new_line = true;
81 	size_t i;
82 
83 	if (in) {
84 		/* This toy device doesn't take in. */
85 		printf("[!] Unexpected IN for the console\n");
86 		exit(EXIT_FAILURE);
87 	}
88 
89 	for (i = 0; i < size; i++) {
90 		if (new_line) {
91 			printf("mach>\t");
92 			new_line = false;
93 		}
94 		printf("%c", (char)buf[i]);
95 		new_line = (buf[i] == '\n');
96 	}
97 
98 	return 0;
99 }
100 
101 /* -------------------------------------------------------------------------- */
102 
103 void
104 toydev_mmio(gpaddr_t gpa, bool write, uint8_t *buf, size_t size)
105 {
106 	/*
107 	 * Dispatch MMIO requests to the proper device.
108 	 */
109 #define LAPIC_START		0xfee00000
110 #define LAPIC_END		0xfee01000
111 	if (gpa >= LAPIC_START && gpa + size <= LAPIC_END) {
112 		toydev_lapic(gpa, write, buf, size);
113 		return;
114 	}
115 
116 	printf("[!] Unknown MMIO device GPA=%p\n", (void *)gpa);
117 	exit(EXIT_FAILURE);
118 }
119 
120 void
121 toydev_io(int port, bool in, uint8_t *buf, size_t size)
122 {
123 	/*
124 	 * Dispatch IO requests to the proper device.
125 	 */
126 	if (port == 123) {
127 		toydev_cons(port, in, buf, size);
128 		return;
129 	}
130 
131 	printf("[!] Unknown IO device PORT=%d\n", port);
132 	exit(EXIT_FAILURE);
133 }
134 
135