Fix metal bugs so deathstar.com runs in qemu (#20)

- Remove XD bit in page tables
- Fix cylinder+head+sector arithmetic
- Implement fstat() for serial file descriptors on metal

Here's how to boot an Actually Portable Executable in QEMU:

    make -j12 o//tool/viz/deathstar.com
    qemu-system-x86_64 -serial stdio -fda o//tool/viz/deathstar.com

Here's a screenshot of DEATHSTAR.COM booted in QEMU:
https://justine.lol/cosmopolitan/cosmo-metal-qemu.png

Thus metal support is in much better shape now, but still incomplete.
Only a few system calls have been polyfilled. To figure out which ones
your program needs, simply boot it in the blinkenlights emulator with a
breakpoint, and press CTRL-C to continue to the system call breakpoint.
If it doesn't break then you should be good. (Note: to emulate normally
you can press 'c' and use CTRL-T and ALT-T to tune the speed.)

    m=tiny
    make -j12 SILENT=0 MODE=$m          \
      o/$m/tool/build/blinkenlights.com \
      o/$m/tool/viz/deathstar.com
    o/$m/tool/build/blinkenlights.com   \
      -r -t -b systemfive.linux         \
      o/$m/tool/viz/deathstar.com

Thank @Theldus for the bug report that made this change possible.
Fixes #20 which explains this change further.
This commit is contained in:
Justine Tunney
2021-01-16 17:52:15 -08:00
parent 58d9659d53
commit f0600a898c
13 changed files with 182 additions and 119 deletions

38
libc/calls/fstat-metal.c Normal file
View File

@ -0,0 +1,38 @@
/*-*- 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│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2021 Justine Alexandra Roberts Tunney │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/internal.h"
#include "libc/calls/struct/stat.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/s.h"
#include "libc/sysv/errfuns.h"
int fstat$metal(int fd, struct stat *st) {
if (fd < 0) return einval();
if (fd < g_fds.n && g_fds.p[fd].kind == kFdSerial) {
memset(st, 0, sizeof(*st));
st->st_dev = g_fds.p[fd].handle;
st->st_rdev = g_fds.p[fd].handle;
st->st_nlink = 1;
st->st_mode = S_IFCHR | 0600;
st->st_blksize = 1;
return 0;
} else {
return ebadf();
}
}

View File

@ -35,34 +35,47 @@ textwindows int fstat$nt(int64_t handle, struct stat *st) {
uint64_t actualsize;
struct NtFileCompressionInfo fci;
struct NtByHandleFileInformation wst;
if (GetFileInformationByHandle(handle, &wst)) {
if ((filetype = GetFileType(handle))) {
memset(st, 0, sizeof(*st));
filetype = GetFileType(handle);
st->st_mode =
(S_IRUSR | S_IXUSR |
(!(wst.dwFileAttributes & kNtFileAttributeReadonly) ? S_IWUSR : 0) |
((wst.dwFileAttributes & kNtFileAttributeNormal) ? S_IFREG : 0) |
((wst.dwFileAttributes & kNtFileFlagOpenReparsePoint) ? S_IFLNK : 0) |
((wst.dwFileAttributes & kNtFileAttributeDirectory)
? S_IFDIR
: (((filetype == kNtFileTypeDisk) ? S_IFBLK : 0) |
((filetype == kNtFileTypeChar) ? S_IFCHR : 0) |
((filetype == kNtFileTypePipe) ? S_IFIFO : 0))));
st->st_atim = FileTimeToTimeSpec(wst.ftLastAccessFileTime);
st->st_mtim = FileTimeToTimeSpec(wst.ftLastWriteFileTime);
st->st_ctim = FileTimeToTimeSpec(wst.ftCreationFileTime);
st->st_size = (uint64_t)wst.nFileSizeHigh << 32 | wst.nFileSizeLow;
st->st_blksize = PAGESIZE;
st->st_dev = wst.dwVolumeSerialNumber;
st->st_ino = (uint64_t)wst.nFileIndexHigh << 32 | wst.nFileIndexLow;
st->st_nlink = wst.nNumberOfLinks;
if (GetFileInformationByHandleEx(handle, kNtFileCompressionInfo, &fci,
sizeof(fci))) {
actualsize = fci.CompressedFileSize;
} else {
actualsize = st->st_size;
switch (filetype) {
case kNtFileTypeChar:
st->st_mode = S_IFCHR | 0600;
break;
case kNtFileTypePipe:
st->st_mode = S_IFIFO | 0600;
break;
case kNtFileTypeDisk:
if (GetFileInformationByHandle(handle, &wst)) {
dprintf(1, "handle = %ld\n", handle);
st->st_mode =
(S_IRUSR | S_IXUSR |
(!(wst.dwFileAttributes & kNtFileAttributeReadonly) ? S_IWUSR
: 0) |
((wst.dwFileAttributes & kNtFileAttributeNormal) ? S_IFREG : 0) |
((wst.dwFileAttributes & kNtFileFlagOpenReparsePoint) ? S_IFLNK
: 0) |
((wst.dwFileAttributes & kNtFileAttributeDirectory) ? S_IFDIR
: 0));
st->st_atim = FileTimeToTimeSpec(wst.ftLastAccessFileTime);
st->st_mtim = FileTimeToTimeSpec(wst.ftLastWriteFileTime);
st->st_ctim = FileTimeToTimeSpec(wst.ftCreationFileTime);
st->st_size = (uint64_t)wst.nFileSizeHigh << 32 | wst.nFileSizeLow;
st->st_blksize = PAGESIZE;
st->st_dev = wst.dwVolumeSerialNumber;
st->st_ino = (uint64_t)wst.nFileIndexHigh << 32 | wst.nFileIndexLow;
st->st_nlink = wst.nNumberOfLinks;
if (GetFileInformationByHandleEx(handle, kNtFileCompressionInfo, &fci,
sizeof(fci))) {
actualsize = fci.CompressedFileSize;
} else {
actualsize = st->st_size;
}
st->st_blocks = roundup(actualsize, PAGESIZE) / 512;
}
break;
default:
break;
}
st->st_blocks = roundup(actualsize, PAGESIZE) / 512;
return 0;
} else {
return __winerr();

View File

@ -32,7 +32,11 @@ int fstat(int fd, struct stat *st) {
return weaken(__zipos_fstat)(
(struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle, st);
} else if (!IsWindows()) {
return fstat$sysv(fd, st);
if (!IsMetal()) {
return fstat$sysv(fd, st);
} else {
return fstat$metal(fd, st);
}
} else {
if (!__isfdkind(fd, kFdFile)) return ebadf();
return fstat$nt(g_fds.p[fd].handle, st);

View File

@ -267,6 +267,12 @@ int64_t __winerr(void) nocallback privileged;
int __mkntpath(const char *, char16_t[hasatleast PATH_MAX - 16]) hidden;
int __mkntpath2(const char *, char16_t[hasatleast PATH_MAX - 16], int) hidden;
/*───────────────────────────────────────────────────────────────────────────│─╗
│ cosmopolitan § syscalls » metal ─╬─│┼
╚────────────────────────────────────────────────────────────────────────────│*/
int fstat$metal(int, struct stat *);
/*───────────────────────────────────────────────────────────────────────────│─╗
│ cosmopolitan § syscalls » drivers ─╬─│┼
╚────────────────────────────────────────────────────────────────────────────│*/

View File

@ -30,16 +30,11 @@
textstartup bool32 ischardev(int fd) {
int olderr;
struct stat st;
if (!IsWindows()) {
olderr = errno;
if (fstat$sysv(fd, &st) != -1) {
return S_ISCHR(st.st_mode);
} else {
errno = olderr;
return false;
}
olderr = errno;
if (fstat(fd, &st) != -1) {
return S_ISCHR(st.st_mode);
} else {
return __isfdkind(fd, kFdFile) &&
GetFileType(g_fds.p[fd].handle) == kNtFileTypeChar;
errno = olderr;
return false;
}
}

View File

@ -31,7 +31,7 @@ forceinline typeof(PrefetchVirtualMemory) *GetPrefetchVirtualMemory(void) {
if (!once) {
once = true;
PrefetchVirtualMemory_ = /* win8.1+ */
GetProcAddressModule("KernelBase.dll", "PrefetchVirtualMemory");
GetProcAddressModule("Kernel32.dll", "PrefetchVirtualMemory");
}
return PrefetchVirtualMemory_;
}
@ -42,7 +42,7 @@ forceinline typeof(OfferVirtualMemory) *GetOfferVirtualMemory(void) {
if (!once) {
once = true;
OfferVirtualMemory_ = /* win8.1+ */
GetProcAddressModule("KernelBase.dll", "OfferVirtualMemory");
GetProcAddressModule("Kernel32.dll", "OfferVirtualMemory");
}
return OfferVirtualMemory_;
}

View File

@ -27,10 +27,14 @@
int nanosleep(const struct timespec *req, struct timespec *rem) {
if (!req) return efault();
if (!IsWindows()) {
if (!IsXnu()) {
return nanosleep$sysv(req, rem);
if (!IsMetal()) {
if (!IsXnu()) {
return nanosleep$sysv(req, rem);
} else {
return nanosleep$xnu(req, rem);
}
} else {
return nanosleep$xnu(req, rem);
return enosys(); /* TODO: Sleep on Metal */
}
} else {
return nanosleep$nt(req, rem);

View File

@ -141,32 +141,36 @@ int(sigaction)(int sig, const struct sigaction *act, struct sigaction *oldact) {
return efault();
}
if (!IsWindows()) {
if (act) {
memcpy(&copy, act, sizeof(copy));
ap = &copy;
if (IsXnu()) {
ap->sa_restorer = (void *)&xnutrampoline;
ap->sa_handler = (void *)&xnutrampoline;
} else {
if (IsLinux()) {
if (!(ap->sa_flags & SA_RESTORER)) {
ap->sa_flags |= SA_RESTORER;
ap->sa_restorer = &__restore_rt;
if (!IsMetal()) {
if (act) {
memcpy(&copy, act, sizeof(copy));
ap = &copy;
if (IsXnu()) {
ap->sa_restorer = (void *)&xnutrampoline;
ap->sa_handler = (void *)&xnutrampoline;
} else {
if (IsLinux()) {
if (!(ap->sa_flags & SA_RESTORER)) {
ap->sa_flags |= SA_RESTORER;
ap->sa_restorer = &__restore_rt;
}
}
if (rva >= kSigactionMinRva) {
ap->sa_sigaction = (sigaction_f)__sigenter;
}
}
if (rva >= kSigactionMinRva) {
ap->sa_sigaction = (sigaction_f)__sigenter;
}
sigaction$cosmo2native((union metasigaction *)ap);
} else {
ap = NULL;
}
sigaction$cosmo2native((union metasigaction *)ap);
rc = sigaction$sysv(
sig, ap, oldact,
(!IsXnu() ? 8 /* or linux whines */
: (int64_t)(intptr_t)oldact /* from go code */));
if (rc != -1) sigaction$native2cosmo((union metasigaction *)oldact);
} else {
ap = NULL;
return enosys(); /* TODO: Signals on Metal */
}
rc = sigaction$sysv(
sig, ap, oldact,
(!IsXnu() ? 8 /* or linux whines */
: (int64_t)(intptr_t)oldact /* from go code */));
if (rc != -1) sigaction$native2cosmo((union metasigaction *)oldact);
} else {
if (oldact) {
memset(oldact, 0, sizeof(*oldact));

View File

@ -28,7 +28,10 @@ program_invocation_short_name:
.init.start 400,_init_program_invocation_short_name
push %rsi
mov (%r13),%rsi
xor %eax,%eax
test %r12d,%r12d # argc
jz 2f
mov (%r13),%rsi # argv[0]
mov %rsi,%rcx
1: lodsb
cmp $'/,%al
@ -39,5 +42,5 @@ program_invocation_short_name:
jnz 1b
xchg %rcx,%rax
pop %rsi
stosq
2: stosq
.init.end 400,_init_program_invocation_short_name