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 <stdbool.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38
39 #include "common.h"
40
41 /* -------------------------------------------------------------------------- */
42
43 /*
44 * LAPIC device, MMIO. We only handle the ID.
45 */
46 static void
toydev_lapic(gpaddr_t gpa,bool write,uint8_t * buf,size_t size)47 toydev_lapic(gpaddr_t gpa, bool write, uint8_t *buf, size_t size)
48 {
49 uint32_t *data;
50
51 #define LAPIC_BASE 0xfee00000
52 #define LAPIC_ID 0x020
53 if (write) {
54 printf("[!] Unexpected LAPIC write!\n");
55 exit(EXIT_FAILURE);
56 }
57 if (size != sizeof(uint32_t)) {
58 printf("[!] Unexpected LAPIC read size %zu!\n", size);
59 exit(EXIT_FAILURE);
60 }
61 if (gpa == LAPIC_BASE + LAPIC_ID) {
62 data = (uint32_t *)&buf[3];
63 *data = 120;
64 }
65 }
66
67 /*
68 * Console device, IO. It retrieves a string on port 123, and we display it.
69 */
70 static int
toydev_cons(int port __unused,bool in,uint8_t * buf,size_t size)71 toydev_cons(int port __unused, bool in, uint8_t *buf, size_t size)
72 {
73 static bool new_line = true;
74 size_t i;
75
76 if (in) {
77 /* This toy device doesn't take in. */
78 printf("[!] Unexpected IN for the console\n");
79 exit(EXIT_FAILURE);
80 }
81
82 for (i = 0; i < size; i++) {
83 if (new_line) {
84 printf("mach>\t");
85 new_line = false;
86 }
87 printf("%c", (char)buf[i]);
88 new_line = (buf[i] == '\n');
89 }
90
91 return 0;
92 }
93
94 /* -------------------------------------------------------------------------- */
95
96 void
toydev_mmio(gpaddr_t gpa,bool write,uint8_t * buf,size_t size)97 toydev_mmio(gpaddr_t gpa, bool write, uint8_t *buf, size_t size)
98 {
99 /*
100 * Dispatch MMIO requests to the proper device.
101 */
102 #define LAPIC_START 0xfee00000
103 #define LAPIC_END 0xfee01000
104 if (gpa >= LAPIC_START && gpa + size <= LAPIC_END) {
105 toydev_lapic(gpa, write, buf, size);
106 return;
107 }
108
109 printf("[!] Unknown MMIO device GPA=%p\n", (void *)gpa);
110 exit(EXIT_FAILURE);
111 }
112
113 void
toydev_io(int port,bool in,uint8_t * buf,size_t size)114 toydev_io(int port, bool in, uint8_t *buf, size_t size)
115 {
116 /*
117 * Dispatch IO requests to the proper device.
118 */
119 if (port == 123) {
120 toydev_cons(port, in, buf, size);
121 return;
122 }
123
124 printf("[!] Unknown IO device PORT=%d\n", port);
125 exit(EXIT_FAILURE);
126 }
127
128