Add VGA support on bare metal (#588)

If your main module has this declaration:

    STATIC_YOINK("vga_console");

Then a VGA driver will be linked into your executable which
displays your stdio characters on the PC display, whereas
before we could only use the serial port. Your display is an
ANSI terminal and it's still a work in progress.
This commit is contained in:
tkchia 2022-09-07 09:41:08 +08:00 committed by GitHub
parent 598640864a
commit 3fdb1c14f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 1556 additions and 5 deletions

View file

@ -117,7 +117,8 @@ include third_party/xed/xed.mk # │
include third_party/zlib/zlib.mk # │
include libc/elf/elf.mk # │
include ape/ape.mk # │
include libc/fmt/fmt.mk #─┘
include libc/fmt/fmt.mk # │
include libc/vga/vga.mk #─┘
include libc/calls/calls.mk #─┐
include libc/runtime/runtime.mk # ├──SYSTEMS RUNTIME
include libc/crt/crt.mk # │ You can issue system calls
@ -318,7 +319,8 @@ COSMOPOLITAN_OBJECTS = \
LIBC_SYSV \
LIBC_INTRIN \
LIBC_NT_KERNEL32 \
LIBC_NEXGEN32E
LIBC_NEXGEN32E \
LIBC_VGA
COSMOPOLITAN_HEADERS = \
APE \
@ -343,6 +345,7 @@ COSMOPOLITAN_HEADERS = \
LIBC_TINYMATH \
LIBC_X \
LIBC_ZIPOS \
LIBC_VGA \
NET_HTTP \
THIRD_PARTY_DLMALLOC \
THIRD_PARTY_GDTOA \

View file

@ -66,6 +66,7 @@ EXAMPLES_DIRECTDEPS = \
LIBC_THREAD \
LIBC_TIME \
LIBC_TINYMATH \
LIBC_VGA \
LIBC_X \
LIBC_ZIPOS \
NET_HTTP \

View file

@ -10,6 +10,8 @@
#include "libc/math.h"
#include "libc/stdio/stdio.h"
STATIC_YOINK("vga_console");
int main(int argc, char *argv[]) {
volatile long double x = -.5;
volatile long double y = 1.5;

View file

@ -19,10 +19,16 @@
#include "libc/calls/struct/fd.internal.h"
#include "libc/calls/struct/iovec.h"
#include "libc/calls/struct/iovec.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/sysv/errfuns.h"
#include "libc/vga/vga.internal.h"
ssize_t sys_writev_metal(struct Fd *fd, const struct iovec *iov, int iovlen) {
switch (fd->kind) {
case kFdConsole:
if (weaken(sys_writev_vga))
weaken(sys_writev_vga)(fd, iov, iovlen);
/* fallthrough */
case kFdSerial:
return sys_writev_serial(fd, iov, iovlen);
default:

View file

@ -21,6 +21,7 @@
#include "libc/intrin/pthread.h"
#include "libc/intrin/pushpop.h"
#include "libc/intrin/spinlock.h"
#include "libc/intrin/weaken.h"
#include "libc/nt/runtime.h"
#include "libc/sysv/consts/o.h"
@ -55,10 +56,17 @@ textstartup void InitializeFileDescriptors(void) {
fds->f = 3;
fds->p = fds->__init_p;
if (IsMetal()) {
extern const char vga_console[];
pushmov(&fds->f, 3ull);
fds->__init_p[0].kind = pushpop(kFdSerial);
fds->__init_p[1].kind = pushpop(kFdSerial);
fds->__init_p[2].kind = pushpop(kFdSerial);
if (weaken(vga_console)) {
fds->__init_p[0].kind = pushpop(kFdConsole);
fds->__init_p[1].kind = pushpop(kFdConsole);
fds->__init_p[2].kind = pushpop(kFdConsole);
} else {
fds->__init_p[0].kind = pushpop(kFdSerial);
fds->__init_p[1].kind = pushpop(kFdSerial);
fds->__init_p[2].kind = pushpop(kFdSerial);
}
fds->__init_p[0].handle = VEIL("r", 0x3F8ull);
fds->__init_p[1].handle = VEIL("r", 0x3F8ull);
fds->__init_p[2].handle = VEIL("r", 0x3F8ull);

View file

@ -30,6 +30,7 @@ o/$(MODE)/libc: o/$(MODE)/libc/calls \
o/$(MODE)/libc/thread \
o/$(MODE)/libc/time \
o/$(MODE)/libc/tinymath \
o/$(MODE)/libc/vga \
o/$(MODE)/libc/x \
o/$(MODE)/libc/zipos \
$(LIBC_CHECKS)

1216
libc/vga/tty.c Normal file

File diff suppressed because it is too large Load diff

66
libc/vga/vga-init.S Normal file
View file

@ -0,0 +1,66 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/macros.internal.h"
// Code snippet for initializing the VGA video mode for bare metal.
//
// If a program requests VGA support (by yoinking vga_console),
// and it is started in bare metal mode, then try to ensure that
// the VGA monitor is in a known mode. This is easier to do while
// the program is still running in real mode.
//
// This module also ropes in the sys_writev_vga routine, which
// implements the actual VGA console output under x86-64 long mode.
//
// @see rlinit & .sort.text.real.init.* (ape/ape.S)
// @see ape/ape.lds
// @see sys_writev_vga (libc/vga/writev-vga.c)
.section .sort.text.real.init.2,"ax",@progbits
.code16
mov $0x4f03,%ax # get current video mode via VESA
int $0x10
cmp $0x004f,%ax # is VESA a thing here?
jz 1f
mov $0x0f,%ah # if not, get the video mode via a
int $0x10 # classical BIOS call
cbtw
xchgw %ax,%bx
1: mov $0x0003,%ax # check if we are in a 80 × ? × 16
cmp %ax,%bx # text mode
jnz 2f
cmpb $25-1,0x0484 # check if number of screen rows
jnz 2f # (BDA.ROWS + 1) is 25; if so, then
mov $0x0500,%ax # just make sure we are on display
# page 0
2: int $0x10 # otherwise, change the video mode
.previous
.code64
.section .rodata,"a",@progbits
vga_console:
.endobj vga_console,globl,hidden
.previous
.yoink sys_writev_vga

118
libc/vga/vga.internal.h Normal file
View file

@ -0,0 +1,118 @@
#ifndef COSMOPOLITAN_LIBC_VGA_VGA_INTERNAL_H_
#define COSMOPOLITAN_LIBC_VGA_VGA_INTERNAL_H_
/*
* VGA_TTY_HEIGHT, VGA_TTY_WIDTH, & VGA_USE_WCS are configuration knobs
* which can potentially be used to tweak the features to be compiled into
* our VGA teletypewriter support.
*/
/**
* Height of the video screen, in character units. Undefine if the height
* may vary at runtime.
*/
#define VGA_TTY_HEIGHT 25
/**
* Width of the video screen, in character units. Undefine if the width may
* vary at runtime.
*/
#define VGA_TTY_WIDTH 80
/**
* If VGA_USE_WCS is defined, the tty code can maintain an array of the
* Unicode characters "underlying" the 8-bit (or 9-bit) characters that are
* actually displayed on the text screen. This can be used to implement
* something similar to Linux's /dev/vcsu* facility.
*
* @see lkml.kernel.org/lkml/204888.1529277815@turing-police.cc.vt.edu/T/
*/
#undef VGA_USE_WCS
#define kTtyFg 0x0001
#define kTtyBg 0x0002
#define kTtyBold 0x0004
#define kTtyFlip 0x0008
#define kTtyFaint 0x0010
#define kTtyUnder 0x0020
#define kTtyDunder 0x0040
#define kTtyTrue 0x0080
#define kTtyBlink 0x0100
#define kTtyItalic 0x0200
#define kTtyFraktur 0x0400
#define kTtyStrike 0x0800
#define kTtyConceal 0x1000
#define kTtyBell 0x001
#define kTtyRedzone 0x002
#define kTtyNocursor 0x004
#define kTtyBlinkcursor 0x008
#define kTtyNocanon 0x010
#define kTtyNoecho 0x020
#define kTtyNoopost 0x040
#define kTtyLed1 0x080
#define kTtyLed2 0x100
#define kTtyLed3 0x200
#define kTtyLed4 0x400
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#include "libc/calls/struct/fd.internal.h"
#include "libc/calls/struct/iovec.h"
#include "libc/calls/struct/iovec.internal.h"
COSMOPOLITAN_C_START_
struct VgaTextCharCell {
uint8_t ch, attr;
};
struct Tty {
unsigned short y, x;
#ifndef VGA_TTY_HEIGHT
unsigned short yn;
#endif
#ifndef VGA_TTY_WIDTH
unsigned short xn;
#endif
uint32_t u8;
uint32_t n8;
uint32_t pr;
uint8_t fg, bg;
uint32_t conf;
unsigned short savey, savex;
struct VgaTextCharCell *ccs;
#ifdef VGA_USE_WCS
wchar_t *wcs;
#endif
wchar_t *xlat;
enum TtyState {
kTtyAscii,
kTtyUtf8,
kTtyEsc,
kTtyCsi,
} state;
struct TtyEsc {
unsigned i;
char s[64];
} esc;
struct TtyInput {
size_t i;
char p[256];
} input;
};
void _StartTty(struct Tty *, unsigned short, unsigned short,
unsigned short, unsigned short, void *, wchar_t *);
ssize_t _TtyRead(struct Tty *, void *, size_t);
ssize_t _TtyWrite(struct Tty *, const void *, size_t);
ssize_t _TtyWriteInput(struct Tty *, const void *, size_t);
void _TtyResetOutputMode(struct Tty *);
void _TtyFullReset(struct Tty *);
void _TtyMemmove(struct Tty *, size_t, size_t, size_t);
void _TtyErase(struct Tty *, size_t, size_t);
void _TtySetY(struct Tty *, unsigned short);
void _TtySetX(struct Tty *, unsigned short);
ssize_t sys_writev_vga(struct Fd *, const struct iovec *, int);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_VGA_VGA_INTERNAL_H_ */

58
libc/vga/vga.mk Normal file
View file

@ -0,0 +1,58 @@
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘
PKGS += LIBC_VGA
LIBC_VGA_ARTIFACTS += LIBC_VGA_A
LIBC_VGA_A = o/$(MODE)/libc/vga/vga.a
LIBC_VGA_A_FILES := $(wildcard libc/vga/*)
LIBC_VGA_A_HDRS = $(filter %.h,$(LIBC_VGA_A_FILES))
LIBC_VGA_A_SRCS_S = $(filter %.S,$(LIBC_VGA_A_FILES))
LIBC_VGA_A_SRCS_C = $(filter %.c,$(LIBC_VGA_A_FILES))
LIBC_VGA = \
$(LIBC_VGA_A_DEPS) \
$(LIBC_VGA_A)
LIBC_VGA_A_SRCS = \
$(LIBC_VGA_A_SRCS_S) \
$(LIBC_VGA_A_SRCS_C)
LIBC_VGA_A_OBJS = \
$(LIBC_VGA_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(LIBC_VGA_A_SRCS_C:%.c=o/$(MODE)/%.o)
LIBC_VGA_A_CHECKS = \
$(LIBC_VGA_A).pkg \
$(LIBC_VGA_A_HDRS:%=o/$(MODE)/%.ok)
LIBC_VGA_A_DIRECTDEPS = \
LIBC_NEXGEN32E \
LIBC_SYSV \
LIBC_STR \
LIBC_INTRIN \
LIBC_STUBS \
LIBC_FMT
LIBC_VGA_A_DEPS := \
$(call uniq,$(foreach x,$(LIBC_VGA_A_DIRECTDEPS),$($(x))))
$(LIBC_VGA_A):libc/vga/ \
$(LIBC_VGA_A).pkg \
$(LIBC_VGA_A_OBJS)
$(LIBC_VGA_A).pkg: \
$(LIBC_VGA_A_OBJS) \
$(foreach x,$(LIBC_VGA_A_DIRECTDEPS),$($(x)_A).pkg)
LIBC_VGA_LIBS = $(foreach x,$(LIBC_VGA_ARTIFACTS),$($(x)))
LIBC_VGA_SRCS = $(foreach x,$(LIBC_VGA_ARTIFACTS),$($(x)_SRCS))
LIBC_VGA_HDRS = $(foreach x,$(LIBC_VGA_ARTIFACTS),$($(x)_HDRS))
LIBC_VGA_BINS = $(foreach x,$(LIBC_VGA_ARTIFACTS),$($(x)_BINS))
LIBC_VGA_CHECKS = $(foreach x,$(LIBC_VGA_ARTIFACTS),$($(x)_CHECKS))
LIBC_VGA_OBJS = $(foreach x,$(LIBC_VGA_ARTIFACTS),$($(x)_OBJS))
LIBC_VGA_TESTS = $(foreach x,$(LIBC_VGA_ARTIFACTS),$($(x)_TESTS))
$(LIBC_VGA_OBJS): $(BUILD_FILES) libc/vga/vga.mk
.PHONY: o/$(MODE)/libc/vga
o/$(MODE)/libc/vga: $(LIBC_VGA_CHECKS)

72
libc/vga/writev-vga.c Normal file
View file

@ -0,0 +1,72 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/calls/struct/fd.internal.h"
#include "libc/calls/struct/iovec.h"
#include "libc/calls/struct/iovec.internal.h"
#include "libc/vga/vga.internal.h"
#include "libc/runtime/pc.internal.h"
#include "libc/str/str.h"
static struct Tty vga_tty;
#ifdef VGA_USE_WCS
static wchar_t vga_wcs[VGA_TTY_HEIGHT * VGA_TTY_WIDTH];
#else
static wchar_t * const vga_wcs = NULL;
#endif
ssize_t sys_writev_vga(struct Fd *fd, const struct iovec *iov, int iovlen) {
size_t i, wrote = 0;
ssize_t res = 0;
for (i = 0; i < iovlen; ++i) {
void *input = iov[i].iov_base;
size_t len = iov[i].iov_len;
res = _TtyWrite(&vga_tty, input, len);
if (res < 0)
break;
wrote += res;
if (res != len)
return wrote;
}
if (!wrote)
return res;
return wrote;
}
__attribute__((__constructor__)) static textstartup void _vga_init(void) {
void * const vid_buf = (void *)(BANE + 0xb8000ull);
/* Get the initial cursor position from the BIOS data area. */
typedef struct {
unsigned char col, row;
} bios_curs_pos_t;
bios_curs_pos_t pos = *(bios_curs_pos_t *)(BANE + 0x0450ull);
/*
* Initialize our tty structure from the current screen contents & current
* cursor position.
*/
_StartTty(&vga_tty, VGA_TTY_HEIGHT, VGA_TTY_WIDTH, pos.row, pos.col,
vid_buf, vga_wcs);
}