1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2015 Nahanni Systems, Inc. 5 * Copyright 2018 Joyent, Inc. 6 * Copyright 2021 OmniOS Community Edition (OmniOSce) Association. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $FreeBSD$ 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <sys/types.h> 37 #include <sys/mman.h> 38 39 #include <machine/vmm.h> 40 #include <vmmapi.h> 41 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 46 #include <errno.h> 47 #include <unistd.h> 48 49 #include "bhyvegc.h" 50 #include "bhyverun.h" 51 #include "config.h" 52 #include "debug.h" 53 #include "console.h" 54 #include "inout.h" 55 #include "pci_emul.h" 56 #include "rfb.h" 57 #include "vga.h" 58 59 /* 60 * bhyve Framebuffer device emulation. 61 * BAR0 points to the current mode information. 62 * BAR1 is the 32-bit framebuffer address. 63 * 64 * -s <b>,fbuf,wait,vga=on|io|off,rfb=<ip>:port,w=width,h=height 65 */ 66 67 static int fbuf_debug = 1; 68 #define DEBUG_INFO 1 69 #define DEBUG_VERBOSE 4 70 #define DPRINTF(level, params) if (level <= fbuf_debug) PRINTLN params 71 72 73 #define KB (1024UL) 74 #define MB (1024 * 1024UL) 75 76 #define DMEMSZ 128 77 78 #define FB_SIZE (16*MB) 79 80 #define COLS_MAX 1920 81 #define ROWS_MAX 1200 82 83 #define COLS_DEFAULT 1024 84 #define ROWS_DEFAULT 768 85 86 #define COLS_MIN 640 87 #define ROWS_MIN 480 88 89 struct pci_fbuf_softc { 90 struct pci_devinst *fsc_pi; 91 struct { 92 uint32_t fbsize; 93 uint16_t width; 94 uint16_t height; 95 uint16_t depth; 96 uint16_t refreshrate; 97 uint8_t reserved[116]; 98 } __packed memregs; 99 100 /* rfb server */ 101 char *rfb_host; 102 char *rfb_password; 103 int rfb_port; 104 #ifndef __FreeBSD__ 105 const char *rfb_unix; 106 #endif 107 int rfb_wait; 108 int vga_enabled; 109 int vga_full; 110 111 uint32_t fbaddr; 112 char *fb_base; 113 uint16_t gc_width; 114 uint16_t gc_height; 115 void *vgasc; 116 struct bhyvegc_image *gc_image; 117 }; 118 119 static struct pci_fbuf_softc *fbuf_sc; 120 121 #define PCI_FBUF_MSI_MSGS 4 122 123 static void 124 pci_fbuf_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, 125 int baridx, uint64_t offset, int size, uint64_t value) 126 { 127 struct pci_fbuf_softc *sc; 128 uint8_t *p; 129 130 assert(baridx == 0); 131 132 sc = pi->pi_arg; 133 134 DPRINTF(DEBUG_VERBOSE, 135 ("fbuf wr: offset 0x%lx, size: %d, value: 0x%lx", 136 offset, size, value)); 137 138 if (offset + size > DMEMSZ) { 139 printf("fbuf: write too large, offset %ld size %d\n", 140 offset, size); 141 return; 142 } 143 144 p = (uint8_t *)&sc->memregs + offset; 145 146 switch (size) { 147 case 1: 148 *p = value; 149 break; 150 case 2: 151 *(uint16_t *)p = value; 152 break; 153 case 4: 154 *(uint32_t *)p = value; 155 break; 156 case 8: 157 *(uint64_t *)p = value; 158 break; 159 default: 160 printf("fbuf: write unknown size %d\n", size); 161 break; 162 } 163 164 if (!sc->gc_image->vgamode && sc->memregs.width == 0 && 165 sc->memregs.height == 0) { 166 DPRINTF(DEBUG_INFO, ("switching to VGA mode")); 167 sc->gc_image->vgamode = 1; 168 sc->gc_width = 0; 169 sc->gc_height = 0; 170 } else if (sc->gc_image->vgamode && sc->memregs.width != 0 && 171 sc->memregs.height != 0) { 172 DPRINTF(DEBUG_INFO, ("switching to VESA mode")); 173 sc->gc_image->vgamode = 0; 174 } 175 } 176 177 uint64_t 178 pci_fbuf_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, 179 int baridx, uint64_t offset, int size) 180 { 181 struct pci_fbuf_softc *sc; 182 uint8_t *p; 183 uint64_t value; 184 185 assert(baridx == 0); 186 187 sc = pi->pi_arg; 188 189 190 if (offset + size > DMEMSZ) { 191 printf("fbuf: read too large, offset %ld size %d\n", 192 offset, size); 193 return (0); 194 } 195 196 p = (uint8_t *)&sc->memregs + offset; 197 value = 0; 198 switch (size) { 199 case 1: 200 value = *p; 201 break; 202 case 2: 203 value = *(uint16_t *)p; 204 break; 205 case 4: 206 value = *(uint32_t *)p; 207 break; 208 case 8: 209 value = *(uint64_t *)p; 210 break; 211 default: 212 printf("fbuf: read unknown size %d\n", size); 213 break; 214 } 215 216 DPRINTF(DEBUG_VERBOSE, 217 ("fbuf rd: offset 0x%lx, size: %d, value: 0x%lx", 218 offset, size, value)); 219 220 return (value); 221 } 222 223 static void 224 pci_fbuf_baraddr(struct vmctx *ctx, struct pci_devinst *pi, int baridx, 225 int enabled, uint64_t address) 226 { 227 struct pci_fbuf_softc *sc; 228 int prot; 229 230 if (baridx != 1) 231 return; 232 233 sc = pi->pi_arg; 234 if (!enabled && sc->fbaddr != 0) { 235 if (vm_munmap_memseg(ctx, sc->fbaddr, FB_SIZE) != 0) 236 EPRINTLN("pci_fbuf: munmap_memseg failed"); 237 sc->fbaddr = 0; 238 } else if (sc->fb_base != NULL && sc->fbaddr == 0) { 239 prot = PROT_READ | PROT_WRITE; 240 if (vm_mmap_memseg(ctx, address, VM_FRAMEBUFFER, 0, FB_SIZE, prot) != 0) 241 EPRINTLN("pci_fbuf: mmap_memseg failed"); 242 sc->fbaddr = address; 243 } 244 } 245 246 247 static int 248 pci_fbuf_parse_config(struct pci_fbuf_softc *sc, nvlist_t *nvl) 249 { 250 const char *value; 251 char *cp; 252 253 sc->rfb_wait = get_config_bool_node_default(nvl, "wait", false); 254 255 /* Prefer "rfb" to "tcp". */ 256 value = get_config_value_node(nvl, "rfb"); 257 if (value == NULL) 258 value = get_config_value_node(nvl, "tcp"); 259 if (value != NULL) { 260 /* 261 * IPv4 -- host-ip:port 262 * IPv6 -- [host-ip%zone]:port 263 * XXX for now port is mandatory for IPv4. 264 */ 265 if (value[0] == '[') { 266 cp = strchr(value + 1, ']'); 267 if (cp == NULL || cp == value + 1) { 268 EPRINTLN("fbuf: Invalid IPv6 address: \"%s\"", 269 value); 270 return (-1); 271 } 272 sc->rfb_host = strndup(value + 1, cp - (value + 1)); 273 cp++; 274 if (*cp == ':') { 275 cp++; 276 if (*cp == '\0') { 277 EPRINTLN( 278 "fbuf: Missing port number: \"%s\"", 279 value); 280 return (-1); 281 } 282 sc->rfb_port = atoi(cp); 283 } else if (*cp != '\0') { 284 EPRINTLN("fbuf: Invalid IPv6 address: \"%s\"", 285 value); 286 return (-1); 287 } 288 } else { 289 cp = strchr(value, ':'); 290 if (cp == NULL) { 291 sc->rfb_port = atoi(value); 292 } else { 293 sc->rfb_host = strndup(value, cp - value); 294 cp++; 295 if (*cp == '\0') { 296 EPRINTLN( 297 "fbuf: Missing port number: \"%s\"", 298 value); 299 return (-1); 300 } 301 sc->rfb_port = atoi(cp); 302 } 303 } 304 } 305 306 #ifndef __FreeBSD__ 307 sc->rfb_unix = get_config_value_node(nvl, "unix"); 308 #endif 309 310 value = get_config_value_node(nvl, "vga"); 311 if (value != NULL) { 312 if (strcmp(value, "off") == 0) { 313 sc->vga_enabled = 0; 314 } else if (strcmp(value, "io") == 0) { 315 sc->vga_enabled = 1; 316 sc->vga_full = 0; 317 } else if (strcmp(value, "on") == 0) { 318 sc->vga_enabled = 1; 319 sc->vga_full = 1; 320 } else { 321 EPRINTLN("fbuf: Invalid vga setting: \"%s\"", value); 322 return (-1); 323 } 324 } 325 326 value = get_config_value_node(nvl, "w"); 327 if (value != NULL) { 328 sc->memregs.width = atoi(value); 329 if (sc->memregs.width > COLS_MAX) { 330 EPRINTLN("fbuf: width %d too large", sc->memregs.width); 331 return (-1); 332 } 333 if (sc->memregs.width == 0) 334 sc->memregs.width = 1920; 335 } 336 337 value = get_config_value_node(nvl, "h"); 338 if (value != NULL) { 339 sc->memregs.height = atoi(value); 340 if (sc->memregs.height > ROWS_MAX) { 341 EPRINTLN("fbuf: height %d too large", 342 sc->memregs.height); 343 return (-1); 344 } 345 if (sc->memregs.height == 0) 346 sc->memregs.height = 1080; 347 } 348 349 value = get_config_value_node(nvl, "password"); 350 if (value != NULL) 351 sc->rfb_password = strdup(value); 352 353 return (0); 354 } 355 356 357 extern void vga_render(struct bhyvegc *gc, void *arg); 358 359 void 360 pci_fbuf_render(struct bhyvegc *gc, void *arg) 361 { 362 struct pci_fbuf_softc *sc; 363 364 sc = arg; 365 366 if (sc->vga_full && sc->gc_image->vgamode) { 367 /* TODO: mode switching to vga and vesa should use the special 368 * EFI-bhyve protocol port. 369 */ 370 vga_render(gc, sc->vgasc); 371 return; 372 } 373 if (sc->gc_width != sc->memregs.width || 374 sc->gc_height != sc->memregs.height) { 375 bhyvegc_resize(gc, sc->memregs.width, sc->memregs.height); 376 sc->gc_width = sc->memregs.width; 377 sc->gc_height = sc->memregs.height; 378 } 379 380 return; 381 } 382 383 static int 384 pci_fbuf_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl) 385 { 386 int error, prot; 387 struct pci_fbuf_softc *sc; 388 389 if (fbuf_sc != NULL) { 390 EPRINTLN("Only one frame buffer device is allowed."); 391 return (-1); 392 } 393 394 sc = calloc(1, sizeof(struct pci_fbuf_softc)); 395 396 pi->pi_arg = sc; 397 398 /* initialize config space */ 399 pci_set_cfgdata16(pi, PCIR_DEVICE, 0x40FB); 400 pci_set_cfgdata16(pi, PCIR_VENDOR, 0xFB5D); 401 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_DISPLAY); 402 pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_DISPLAY_VGA); 403 404 error = pci_emul_alloc_bar(pi, 0, PCIBAR_MEM32, DMEMSZ); 405 assert(error == 0); 406 407 error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, FB_SIZE); 408 assert(error == 0); 409 410 error = pci_emul_add_msicap(pi, PCI_FBUF_MSI_MSGS); 411 assert(error == 0); 412 413 sc->fbaddr = pi->pi_bar[1].addr; 414 sc->memregs.fbsize = FB_SIZE; 415 sc->memregs.width = COLS_DEFAULT; 416 sc->memregs.height = ROWS_DEFAULT; 417 sc->memregs.depth = 32; 418 419 sc->vga_enabled = 1; 420 sc->vga_full = 0; 421 422 sc->fsc_pi = pi; 423 424 error = pci_fbuf_parse_config(sc, nvl); 425 if (error != 0) 426 goto done; 427 428 /* XXX until VGA rendering is enabled */ 429 if (sc->vga_full != 0) { 430 EPRINTLN("pci_fbuf: VGA rendering not enabled"); 431 goto done; 432 } 433 434 sc->fb_base = vm_create_devmem(ctx, VM_FRAMEBUFFER, "framebuffer", FB_SIZE); 435 if (sc->fb_base == MAP_FAILED) { 436 error = -1; 437 goto done; 438 } 439 DPRINTF(DEBUG_INFO, ("fbuf frame buffer base: %p [sz %lu]", 440 sc->fb_base, FB_SIZE)); 441 442 /* 443 * Map the framebuffer into the guest address space. 444 * XXX This may fail if the BAR is different than a prior 445 * run. In this case flag the error. This will be fixed 446 * when a change_memseg api is available. 447 */ 448 prot = PROT_READ | PROT_WRITE; 449 if (vm_mmap_memseg(ctx, sc->fbaddr, VM_FRAMEBUFFER, 0, FB_SIZE, prot) != 0) { 450 EPRINTLN("pci_fbuf: mapseg failed - try deleting VM and restarting"); 451 error = -1; 452 goto done; 453 } 454 455 console_init(sc->memregs.width, sc->memregs.height, sc->fb_base); 456 console_fb_register(pci_fbuf_render, sc); 457 458 if (sc->vga_enabled) 459 sc->vgasc = vga_init(!sc->vga_full); 460 sc->gc_image = console_get_image(); 461 462 fbuf_sc = sc; 463 464 memset((void *)sc->fb_base, 0, FB_SIZE); 465 466 #ifdef __FreeBSD__ 467 error = rfb_init(sc->rfb_host, sc->rfb_port, sc->rfb_wait, sc->rfb_password); 468 #else 469 char *name; 470 471 (void) asprintf(&name, "%s (bhyve)", get_config_value("name")); 472 473 if (sc->rfb_unix != NULL) { 474 error = rfb_init_unix(sc->rfb_unix, sc->rfb_wait, 475 sc->rfb_password, name != NULL ? name : "bhyve"); 476 } else { 477 error = rfb_init(sc->rfb_host, sc->rfb_port, sc->rfb_wait, 478 sc->rfb_password, name != NULL ? name : "bhyve"); 479 } 480 if (error != 0) 481 free(name); 482 #endif 483 done: 484 if (error) 485 free(sc); 486 487 return (error); 488 } 489 490 struct pci_devemu pci_fbuf = { 491 .pe_emu = "fbuf", 492 .pe_init = pci_fbuf_init, 493 .pe_barwrite = pci_fbuf_write, 494 .pe_barread = pci_fbuf_read, 495 .pe_baraddr = pci_fbuf_baraddr, 496 }; 497 PCI_EMUL_SET(pci_fbuf); 498