Skip to content

Commit eea601f

Browse files
authored
[metal] Some minor fixes and tweaks (#933)
* [metal] Ensure DF is clear when calling C from exception handler * [metal] Mark some internal routines and declarations as `@internal` * [metal] Fix crash under UEFI when command line string is NULL * [metal] Fix argc & argv[] setting, & VM page freeing, for UEFI Part of the memory occupied by the argv[] contents was erroneously used for page tables & then later erroneously freed. The symptom was that argv[0] would show up as an empty string ("").
1 parent 1c2e7c1 commit eea601f

File tree

8 files changed

+76
-20
lines changed

8 files changed

+76
-20
lines changed

libc/intrin/interrupts.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ __excep0_isr:
9292
mov %eax,%ss
9393
mov %eax,%ds
9494
mov %eax,%es
95+
cld # make sure DF is reset, for C code
9596
ezlea .excep_msg,di # stack should be 16-byte aligned now
9697
xor %eax,%eax # kprintf is variadic, remember to
9798
# pass no. of vector regs. used (= 0)

libc/intrin/mman.greg.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ struct ReclaimedPage {
5959
};
6060

6161
/**
62+
* @internal
6263
* Allocates new page of physical memory.
6364
*/
6465
texthead uint64_t __new_page(struct mman *mm) {
@@ -85,6 +86,7 @@ texthead uint64_t __new_page(struct mman *mm) {
8586
}
8687

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

108110
/**
111+
* @internal
109112
* Sorts, rounds, and filters BIOS memory map.
110113
*/
111114
static textreal void __normalize_e820(struct mman *mm, uint64_t top) {
@@ -139,6 +142,7 @@ static textreal void __normalize_e820(struct mman *mm, uint64_t top) {
139142
}
140143

141144
/**
145+
* @internal
142146
* Identity maps an area of physical memory to its negative address.
143147
*/
144148
textreal uint64_t *__invert_memory_area(struct mman *mm, uint64_t *pml4t,
@@ -157,6 +161,7 @@ textreal uint64_t *__invert_memory_area(struct mman *mm, uint64_t *pml4t,
157161
}
158162

159163
/**
164+
* @internal
160165
* Increments the reference count for a page of physical memory.
161166
*/
162167
void __ref_page(struct mman *mm, uint64_t *pml4t, uint64_t p) {
@@ -172,6 +177,7 @@ void __ref_page(struct mman *mm, uint64_t *pml4t, uint64_t p) {
172177
}
173178

174179
/**
180+
* @internal
175181
* Increments the reference counts for an area of physical memory.
176182
*/
177183
void __ref_pages(struct mman *mm, uint64_t *pml4t, uint64_t ps, uint64_t size) {
@@ -183,6 +189,7 @@ void __ref_pages(struct mman *mm, uint64_t *pml4t, uint64_t ps, uint64_t size) {
183189
}
184190

185191
/**
192+
* @internal
186193
* Reclaims a page of physical memory for later use.
187194
*/
188195
static void __reclaim_page(struct mman *mm, uint64_t p) {
@@ -193,6 +200,7 @@ static void __reclaim_page(struct mman *mm, uint64_t p) {
193200
}
194201

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

213221
/**
222+
* @internal
214223
* Identity maps all usable physical memory to its negative address.
215224
*/
216225
static textreal void __invert_memory(struct mman *mm, uint64_t *pml4t) {
@@ -224,6 +233,7 @@ static textreal void __invert_memory(struct mman *mm, uint64_t *pml4t) {
224233
}
225234

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

286296
/**
297+
* @internal
287298
* Maps APE-defined ELF program headers into memory and clears BSS.
288299
*/
289300
textreal void __map_phdrs(struct mman *mm, uint64_t *pml4t, uint64_t b,
@@ -315,6 +326,7 @@ textreal void __map_phdrs(struct mman *mm, uint64_t *pml4t, uint64_t b,
315326
}
316327

317328
/**
329+
* @internal
318330
* Reclaims memory pages which were used at boot time but which can now be
319331
* made available for the application.
320332
*/

libc/irq/acpi.internal.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
#include "libc/log/color.internal.h"
66

77
/**
8+
* @internal
9+
* @fileoverview Declarations for bare metal code to interact with ACPI
10+
*
11+
* @see UEFI Forum, Inc. Advanced Configuration and Power Interface (ACPI)
12+
* Specification, Version 6.5, 2022. https://uefi.org/specifications
13+
* @see Intel Corporation. ACPI Component Architecture: User Guide and
14+
* Programmer Reference, Revision 6.2, 2017. https://acpica.org
15+
*/
16+
17+
/**
18+
* @internal
819
* AcpiStatus values.
920
*/
1021
#define kAcpiOk 0x0000
@@ -13,11 +24,13 @@
1324
#define kAcpiExBadChecksum 0x2003
1425

1526
/**
27+
* @internal
1628
* Flags for AcpiTableMadt::Flags.
1729
*/
1830
#define kAcpiMadtPcAtCompat 0x0001
1931

2032
/**
33+
* @internal
2134
* Flags for AcpiTableFadt::BootFlags.
2235
*/
2336
#define kAcpiFadtLegacyDevices 0x0001
@@ -28,6 +41,7 @@
2841
#define kAcpiFadtNoCmosRtc 0x0020
2942

3043
/**
44+
* @internal
3145
* Values for AcpiSubtableHeader::Type under an AcpiTableMadt.
3246
*/
3347
#define kAcpiMadtLocalApic 0
@@ -36,15 +50,6 @@
3650

3751
#if !(__ASSEMBLER__ + __LINKER__ + 0)
3852

39-
/**
40-
* @fileoverview Declarations for bare metal code to interact with ACPI
41-
*
42-
* @see UEFI Forum, Inc. Advanced Configuration and Power Interface (ACPI)
43-
* Specification, Version 6.5, 2022. https://uefi.org/specifications
44-
* @see Intel Corporation. ACPI Component Architecture: User Guide and
45-
* Programmer Reference, Revision 6.2, 2017. https://acpica.org
46-
*/
47-
4853
COSMOPOLITAN_C_START_
4954

5055
/**

libc/runtime/efimain.greg.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ __msabi EFI_STATUS EfiMain(EFI_HANDLE ImageHandle,
173173
uint64_t Address;
174174
uintptr_t Args, MapKey, DescSize;
175175
uint64_t *pd, *pml4t, *pdt1, *pdt2, *pdpt1, *pdpt2;
176+
const char16_t *CmdLine;
176177

177178
extern char os asm("__hostos");
178179
os = _HOSTMETAL;
@@ -194,26 +195,28 @@ __msabi EFI_STATUS EfiMain(EFI_HANDLE ImageHandle,
194195
Address = 0x79000;
195196
SystemTable->BootServices->AllocatePages(
196197
AllocateAddress, EfiRuntimeServicesData,
197-
(0x7e000 - 0x79000 + sizeof(struct EfiArgs) + 4095) / 4096, &Address);
198+
(0x7f000 - 0x79000 + sizeof(struct EfiArgs) + 4095) / 4096, &Address);
198199
Address = IMAGE_BASE_PHYSICAL;
199200
SystemTable->BootServices->AllocatePages(
200201
AllocateAddress, EfiRuntimeServicesData,
201202
((_end - __executable_start) + 4095) / 4096, &Address);
202203
mm = __get_mm_phy();
203204
SystemTable->BootServices->SetMem(mm, sizeof(*mm), 0);
204205
SystemTable->BootServices->SetMem(
205-
(void *)0x79000, 0x7e000 - 0x79000 + sizeof(struct EfiArgs), 0);
206+
(void *)0x79000, 0x7f000 - 0x79000 + sizeof(struct EfiArgs), 0);
206207
SystemTable->BootServices->CopyMem((void *)IMAGE_BASE_PHYSICAL,
207208
__executable_start,
208209
_end - __executable_start);
209210

210211
/*
211212
* Converts UEFI shell arguments to argv.
212213
*/
213-
ArgBlock = (struct EfiArgs *)0x7e000;
214+
ArgBlock = (struct EfiArgs *)0x7f000;
214215
SystemTable->BootServices->HandleProtocol(ImageHandle,
215216
&kEfiLoadedImageProtocol, &ImgInfo);
216-
Args = GetDosArgv(ImgInfo->LoadOptions, ArgBlock->ArgBlock,
217+
CmdLine = (const char16_t *)ImgInfo->LoadOptions;
218+
if (!CmdLine || !CmdLine[0]) CmdLine = u"BOOTX64.EFI";
219+
Args = GetDosArgv(CmdLine, ArgBlock->ArgBlock,
217220
sizeof(ArgBlock->ArgBlock), ArgBlock->Args,
218221
ARRAYLEN(ArgBlock->Args));
219222

libc/vga/tty-graph.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "libc/vga/vga.internal.h"
3131

3232
/**
33+
* @internal
3334
* @fileoverview Instantiation of routines for normal console output in
3435
* graphical video modes.
3536
*

libc/vga/tty-graph.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "libc/vga/vga.internal.h"
3232

3333
/**
34+
* @internal
3435
* @fileoverview Template for routines to support output in graphical video
3536
* modes for bare metal VGA.
3637
*

libc/vga/tty.greg.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#ifdef __x86_64__
3434

3535
/**
36+
* @internal
3637
* @fileoverview ECMA-48 / VT100 video terminal implementation for bare
3738
* metal VGA.
3839
*
@@ -325,6 +326,7 @@ static void TtySetCodepage(struct Tty *tty, char id) {
325326
}
326327

327328
/**
329+
* @internal
328330
* Map the given direct color value to one of the 16 basic foreground colors
329331
* or one of the 16 background colors.
330332
*
@@ -344,6 +346,7 @@ static uint8_t TtyGetTextColor(TtyCanvasColor color) {
344346
}
345347

346348
/**
349+
* @internal
347350
* Map the currently active foreground & background colors & terminal
348351
* configuration to a VGA text character attribute byte.
349352
*
@@ -794,12 +797,18 @@ static void TtyCsiN(struct Tty *tty) {
794797
}
795798
}
796799

797-
/** Create a direct color pixel value. */
800+
/**
801+
* @internal
802+
* Create a direct color pixel value.
803+
*/
798804
static TtyCanvasColor TtyMapTrueColor(uint8_t r, uint8_t g, uint8_t b) {
799805
return (TtyCanvasColor){.bgr.r = r, .bgr.g = g, .bgr.b = b, .bgr.x = 0xff};
800806
}
801807

802-
/** Map the given 256-color code to a direct color. */
808+
/**
809+
* @internal
810+
* Map the given 256-color code to a direct color.
811+
*/
803812
static TtyCanvasColor TtyMapXtermColor(uint8_t color) {
804813
uint8_t r, g, b;
805814
if (color < 8) {
@@ -822,7 +831,10 @@ static TtyCanvasColor TtyMapXtermColor(uint8_t color) {
822831
return TtyMapTrueColor(r, g, b);
823832
}
824833

825-
/** Map the given ECMA-48 / VT100 SGR color code to a direct color. */
834+
/**
835+
* @internal
836+
* Map the given ECMA-48 / VT100 SGR color code to a direct color.
837+
*/
826838
static TtyCanvasColor TtyMapEcma48Color(uint8_t color) {
827839
return TtyMapXtermColor(color % 16);
828840
}

libc/vga/vga.internal.h

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@
22
#define COSMOPOLITAN_LIBC_VGA_VGA_INTERNAL_H_
33
#include "libc/runtime/mman.internal.h"
44

5-
/** Preferred width of the video screen, in character units. */
5+
/**
6+
* @internal
7+
* Preferred width of the video screen, in character units.
8+
*/
69
#define VGA_PREFER_TTY_HEIGHT 30
7-
/** Preferred width of the video screen, in character units. */
10+
/**
11+
* @internal
12+
* Preferred width of the video screen, in character units.
13+
*/
814
#define VGA_PREFER_TTY_WIDTH 80
9-
/** Assumed height of each character in pixels, in graphics modes. */
15+
/**
16+
* @internal
17+
* Assumed height of each character in pixels, in graphics modes.
18+
*/
1019
#define VGA_ASSUME_CHAR_HEIGHT_PX 16
1120
/** Assumed width of each character in pixels, in graphics modes. */
1221
#define VGA_ASSUME_CHAR_WIDTH_PX 8
@@ -18,6 +27,7 @@
1827
*/
1928

2029
/**
30+
* @internal
2131
* If VGA_USE_WCS is defined, the tty code can maintain an array of the
2232
* Unicode characters "underlying" the 8-bit (or 9-bit) characters that are
2333
* actually displayed on the text screen. This Unicode character
@@ -33,6 +43,7 @@
3343
*/
3444
#undef VGA_USE_WCS
3545
/**
46+
* @internal
3647
* The VGA hardware can be configured — via the IBM BIOS, or via port I/O —
3748
* to either support blinking characters, or support the use of bright
3849
* background colors, but not both. There is a hardware setting that
@@ -67,6 +78,7 @@
6778
*/
6879
#undef VGA_USE_BLINK
6980
/**
81+
* @internal
7082
* If VGA_PERSNICKETY_STATUS is defined, then when deciding how to return
7183
* status response codes (e.g. "\e[0n"), the tty code will pay attention to
7284
* the terminal's termios mode (TODO). If undefined, the tty code will
@@ -75,7 +87,10 @@
7587
*/
7688
#undef VGA_PERSNICKETY_STATUS
7789

78-
/* Flags which are passed to _StartTty(). */
90+
/**
91+
* @internal
92+
* Flags which are passed to _StartTty().
93+
*/
7994
#define kTtyAllocWcs \
8095
0x01 /* allocate Unicode character array \
8196
(if VGA_USE_WCS also defined) */
@@ -85,6 +100,7 @@
85100
to show system messages */
86101

87102
/**
103+
* @internal
88104
* Flags for Tty::pr. These govern properties of individual character cells.
89105
*/
90106
#define kTtyFg 0x0001
@@ -102,6 +118,7 @@
102118
#define kTtyConceal 0x1000
103119

104120
/**
121+
* @internal
105122
* Flags for Tty::conf. These govern the current state of the teletypewriter
106123
* as a whole.
107124
*/
@@ -153,6 +170,10 @@ typedef union {
153170
typedef TtyBgrxColor TtyCanvasColor;
154171
struct Tty;
155172

173+
/**
174+
* @internal
175+
* Video console object type.
176+
*/
156177
struct Tty {
157178
/**
158179
* Cursor position. (y, x) = (0, 0) means the cursor is on the top left

0 commit comments

Comments
 (0)