Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[metal] Some minor fixes and tweaks #933

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/intrin/interrupts.S
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ __excep0_isr:
mov %eax,%ss
mov %eax,%ds
mov %eax,%es
cld # make sure DF is reset, for C code
ezlea .excep_msg,di # stack should be 16-byte aligned now
xor %eax,%eax # kprintf is variadic, remember to
# pass no. of vector regs. used (= 0)
Expand Down
12 changes: 12 additions & 0 deletions libc/intrin/mman.greg.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct ReclaimedPage {
};

/**
* @internal
* Allocates new page of physical memory.
*/
texthead uint64_t __new_page(struct mman *mm) {
Expand All @@ -85,6 +86,7 @@ texthead uint64_t __new_page(struct mman *mm) {
}

/**
* @internal
* Returns pointer to page table entry for page at virtual address.
* Additional page tables are allocated if needed as a side-effect.
*/
Expand All @@ -106,6 +108,7 @@ textreal uint64_t *__get_virtual(struct mman *mm, uint64_t *t, int64_t vaddr,
}

/**
* @internal
* Sorts, rounds, and filters BIOS memory map.
*/
static textreal void __normalize_e820(struct mman *mm, uint64_t top) {
Expand Down Expand Up @@ -139,6 +142,7 @@ static textreal void __normalize_e820(struct mman *mm, uint64_t top) {
}

/**
* @internal
* Identity maps an area of physical memory to its negative address.
*/
textreal uint64_t *__invert_memory_area(struct mman *mm, uint64_t *pml4t,
Expand All @@ -157,6 +161,7 @@ textreal uint64_t *__invert_memory_area(struct mman *mm, uint64_t *pml4t,
}

/**
* @internal
* Increments the reference count for a page of physical memory.
*/
void __ref_page(struct mman *mm, uint64_t *pml4t, uint64_t p) {
Expand All @@ -172,6 +177,7 @@ void __ref_page(struct mman *mm, uint64_t *pml4t, uint64_t p) {
}

/**
* @internal
* Increments the reference counts for an area of physical memory.
*/
void __ref_pages(struct mman *mm, uint64_t *pml4t, uint64_t ps, uint64_t size) {
Expand All @@ -183,6 +189,7 @@ void __ref_pages(struct mman *mm, uint64_t *pml4t, uint64_t ps, uint64_t size) {
}

/**
* @internal
* Reclaims a page of physical memory for later use.
*/
static void __reclaim_page(struct mman *mm, uint64_t p) {
Expand All @@ -193,6 +200,7 @@ static void __reclaim_page(struct mman *mm, uint64_t p) {
}

/**
* @internal
* Decrements the reference count for a page of physical memory. Frees the
* page if there are no virtual addresses (excluding the negative space)
* referring to it.
Expand All @@ -211,6 +219,7 @@ void __unref_page(struct mman *mm, uint64_t *pml4t, uint64_t p) {
}

/**
* @internal
* Identity maps all usable physical memory to its negative address.
*/
static textreal void __invert_memory(struct mman *mm, uint64_t *pml4t) {
Expand All @@ -224,6 +233,7 @@ static textreal void __invert_memory(struct mman *mm, uint64_t *pml4t) {
}

/**
* @internal
* Exports information about the offset of a field within a structure type,
* so that assembly language routines can use it. This macro can be invoked
* from inside a function whose code is known to be emitted.
Expand Down Expand Up @@ -284,6 +294,7 @@ static textreal uint64_t __map_phdr(struct mman *mm, uint64_t *pml4t,
}

/**
* @internal
* Maps APE-defined ELF program headers into memory and clears BSS.
*/
textreal void __map_phdrs(struct mman *mm, uint64_t *pml4t, uint64_t b,
Expand Down Expand Up @@ -315,6 +326,7 @@ textreal void __map_phdrs(struct mman *mm, uint64_t *pml4t, uint64_t b,
}

/**
* @internal
* Reclaims memory pages which were used at boot time but which can now be
* made available for the application.
*/
Expand Down
23 changes: 14 additions & 9 deletions libc/irq/acpi.internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
#include "libc/log/color.internal.h"

/**
* @internal
* @fileoverview Declarations for bare metal code to interact with ACPI
*
* @see UEFI Forum, Inc. Advanced Configuration and Power Interface (ACPI)
* Specification, Version 6.5, 2022. https://uefi.org/specifications
* @see Intel Corporation. ACPI Component Architecture: User Guide and
* Programmer Reference, Revision 6.2, 2017. https://acpica.org
*/

/**
* @internal
* AcpiStatus values.
*/
#define kAcpiOk 0x0000
Expand All @@ -13,11 +24,13 @@
#define kAcpiExBadChecksum 0x2003

/**
* @internal
* Flags for AcpiTableMadt::Flags.
*/
#define kAcpiMadtPcAtCompat 0x0001

/**
* @internal
* Flags for AcpiTableFadt::BootFlags.
*/
#define kAcpiFadtLegacyDevices 0x0001
Expand All @@ -28,6 +41,7 @@
#define kAcpiFadtNoCmosRtc 0x0020

/**
* @internal
* Values for AcpiSubtableHeader::Type under an AcpiTableMadt.
*/
#define kAcpiMadtLocalApic 0
Expand All @@ -36,15 +50,6 @@

#if !(__ASSEMBLER__ + __LINKER__ + 0)

/**
* @fileoverview Declarations for bare metal code to interact with ACPI
*
* @see UEFI Forum, Inc. Advanced Configuration and Power Interface (ACPI)
* Specification, Version 6.5, 2022. https://uefi.org/specifications
* @see Intel Corporation. ACPI Component Architecture: User Guide and
* Programmer Reference, Revision 6.2, 2017. https://acpica.org
*/

COSMOPOLITAN_C_START_

/**
Expand Down
11 changes: 7 additions & 4 deletions libc/runtime/efimain.greg.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ __msabi EFI_STATUS EfiMain(EFI_HANDLE ImageHandle,
uint64_t Address;
uintptr_t Args, MapKey, DescSize;
uint64_t *pd, *pml4t, *pdt1, *pdt2, *pdpt1, *pdpt2;
const char16_t *CmdLine;

extern char os asm("__hostos");
os = _HOSTMETAL;
Expand All @@ -194,26 +195,28 @@ __msabi EFI_STATUS EfiMain(EFI_HANDLE ImageHandle,
Address = 0x79000;
SystemTable->BootServices->AllocatePages(
AllocateAddress, EfiRuntimeServicesData,
(0x7e000 - 0x79000 + sizeof(struct EfiArgs) + 4095) / 4096, &Address);
(0x7f000 - 0x79000 + sizeof(struct EfiArgs) + 4095) / 4096, &Address);
Address = IMAGE_BASE_PHYSICAL;
SystemTable->BootServices->AllocatePages(
AllocateAddress, EfiRuntimeServicesData,
((_end - __executable_start) + 4095) / 4096, &Address);
mm = __get_mm_phy();
SystemTable->BootServices->SetMem(mm, sizeof(*mm), 0);
SystemTable->BootServices->SetMem(
(void *)0x79000, 0x7e000 - 0x79000 + sizeof(struct EfiArgs), 0);
(void *)0x79000, 0x7f000 - 0x79000 + sizeof(struct EfiArgs), 0);
SystemTable->BootServices->CopyMem((void *)IMAGE_BASE_PHYSICAL,
__executable_start,
_end - __executable_start);

/*
* Converts UEFI shell arguments to argv.
*/
ArgBlock = (struct EfiArgs *)0x7e000;
ArgBlock = (struct EfiArgs *)0x7f000;
SystemTable->BootServices->HandleProtocol(ImageHandle,
&kEfiLoadedImageProtocol, &ImgInfo);
Args = GetDosArgv(ImgInfo->LoadOptions, ArgBlock->ArgBlock,
CmdLine = (const char16_t *)ImgInfo->LoadOptions;
if (!CmdLine || !CmdLine[0]) CmdLine = u"BOOTX64.EFI";
Args = GetDosArgv(CmdLine, ArgBlock->ArgBlock,
sizeof(ArgBlock->ArgBlock), ArgBlock->Args,
ARRAYLEN(ArgBlock->Args));

Expand Down
1 change: 1 addition & 0 deletions libc/vga/tty-graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "libc/vga/vga.internal.h"

/**
* @internal
* @fileoverview Instantiation of routines for normal console output in
* graphical video modes.
*
Expand Down
1 change: 1 addition & 0 deletions libc/vga/tty-graph.inc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "libc/vga/vga.internal.h"

/**
* @internal
* @fileoverview Template for routines to support output in graphical video
* modes for bare metal VGA.
*
Expand Down
18 changes: 15 additions & 3 deletions libc/vga/tty.greg.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#ifdef __x86_64__

/**
* @internal
* @fileoverview ECMA-48 / VT100 video terminal implementation for bare
* metal VGA.
*
Expand Down Expand Up @@ -325,6 +326,7 @@ static void TtySetCodepage(struct Tty *tty, char id) {
}

/**
* @internal
* Map the given direct color value to one of the 16 basic foreground colors
* or one of the 16 background colors.
*
Expand All @@ -344,6 +346,7 @@ static uint8_t TtyGetTextColor(TtyCanvasColor color) {
}

/**
* @internal
* Map the currently active foreground & background colors & terminal
* configuration to a VGA text character attribute byte.
*
Expand Down Expand Up @@ -794,12 +797,18 @@ static void TtyCsiN(struct Tty *tty) {
}
}

/** Create a direct color pixel value. */
/**
* @internal
* Create a direct color pixel value.
*/
static TtyCanvasColor TtyMapTrueColor(uint8_t r, uint8_t g, uint8_t b) {
return (TtyCanvasColor){.bgr.r = r, .bgr.g = g, .bgr.b = b, .bgr.x = 0xff};
}

/** Map the given 256-color code to a direct color. */
/**
* @internal
* Map the given 256-color code to a direct color.
*/
static TtyCanvasColor TtyMapXtermColor(uint8_t color) {
uint8_t r, g, b;
if (color < 8) {
Expand All @@ -822,7 +831,10 @@ static TtyCanvasColor TtyMapXtermColor(uint8_t color) {
return TtyMapTrueColor(r, g, b);
}

/** Map the given ECMA-48 / VT100 SGR color code to a direct color. */
/**
* @internal
* Map the given ECMA-48 / VT100 SGR color code to a direct color.
*/
static TtyCanvasColor TtyMapEcma48Color(uint8_t color) {
return TtyMapXtermColor(color % 16);
}
Expand Down
29 changes: 25 additions & 4 deletions libc/vga/vga.internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@
#define COSMOPOLITAN_LIBC_VGA_VGA_INTERNAL_H_
#include "libc/runtime/mman.internal.h"

/** Preferred width of the video screen, in character units. */
/**
* @internal
* Preferred width of the video screen, in character units.
*/
#define VGA_PREFER_TTY_HEIGHT 30
/** Preferred width of the video screen, in character units. */
/**
* @internal
* Preferred width of the video screen, in character units.
*/
#define VGA_PREFER_TTY_WIDTH 80
/** Assumed height of each character in pixels, in graphics modes. */
/**
* @internal
* Assumed height of each character in pixels, in graphics modes.
*/
#define VGA_ASSUME_CHAR_HEIGHT_PX 16
/** Assumed width of each character in pixels, in graphics modes. */
#define VGA_ASSUME_CHAR_WIDTH_PX 8
Expand All @@ -18,6 +27,7 @@
*/

/**
* @internal
* 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 Unicode character
Expand All @@ -33,6 +43,7 @@
*/
#undef VGA_USE_WCS
/**
* @internal
* The VGA hardware can be configured — via the IBM BIOS, or via port I/O —
* to either support blinking characters, or support the use of bright
* background colors, but not both. There is a hardware setting that
Expand Down Expand Up @@ -67,6 +78,7 @@
*/
#undef VGA_USE_BLINK
/**
* @internal
* If VGA_PERSNICKETY_STATUS is defined, then when deciding how to return
* status response codes (e.g. "\e[0n"), the tty code will pay attention to
* the terminal's termios mode (TODO). If undefined, the tty code will
Expand All @@ -75,7 +87,10 @@
*/
#undef VGA_PERSNICKETY_STATUS

/* Flags which are passed to _StartTty(). */
/**
* @internal
* Flags which are passed to _StartTty().
*/
#define kTtyAllocWcs \
0x01 /* allocate Unicode character array \
(if VGA_USE_WCS also defined) */
Expand All @@ -85,6 +100,7 @@
to show system messages */

/**
* @internal
* Flags for Tty::pr. These govern properties of individual character cells.
*/
#define kTtyFg 0x0001
Expand All @@ -102,6 +118,7 @@
#define kTtyConceal 0x1000

/**
* @internal
* Flags for Tty::conf. These govern the current state of the teletypewriter
* as a whole.
*/
Expand Down Expand Up @@ -153,6 +170,10 @@ typedef union {
typedef TtyBgrxColor TtyCanvasColor;
struct Tty;

/**
* @internal
* Video console object type.
*/
struct Tty {
/**
* Cursor position. (y, x) = (0, 0) means the cursor is on the top left
Expand Down