Work towards zipos / open(argv[0]) on metal (#667)

This commit is contained in:
tkchia 2022-11-06 15:29:47 +08:00 committed by GitHub
parent 543c93f623
commit cb9a0466f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 257 additions and 46 deletions

View file

@ -35,6 +35,7 @@
#include "ape/macros.internal.h"
#include "ape/notice.inc"
#include "ape/relocations.h"
#include "libc/calls/metalfile.internal.h"
#include "libc/dce.h"
#include "libc/elf/def.h"
#include "libc/macho.internal.h"
@ -1565,19 +1566,19 @@ kernel: movabs $ape_stack_vaddr,%rsp
#endif
push $_HOSTMETAL # sets __hostos in crt.S
pop %rcx
push $0
pushq .Lenv0(%rip) # envp[0][0]
mov %rsp,%rbp
mov .Lenv0(%rip),%rax
mov %rax,(%rbp) # envp[0][0]
push $0 # argv[0][0]
pushq .Largv0+8(%rip) # argv[0][8]
pushq .Largv0(%rip) # argv[0][0]
mov %rsp,%rax
push $0 # auxv[1][1]
push $0 # auxv[1][0]
push %rbp # auxv[0][1]
push %rax # auxv[0][1]
push $31 # auxv[0][0] AT_EXECFN
push $0 # envp[1]
push $.Lenv0 # envp[0]
push %rbp # envp[0]
push $0 # argv[1]
push %rbp # argv[0]
push %rax # argv[0]
push $1 # argc
xor %ebp,%ebp
xor %eax,%eax
@ -1593,6 +1594,9 @@ kernel: movabs $ape_stack_vaddr,%rsp
.rodata
.Lenv0: .asciz "METAL=1"
.Largv0:
.asciz APE_COM_NAME
.org .Largv0+16
.previous
#endif /* SupportsMetal() */

View file

@ -565,10 +565,10 @@ SHSTUB2(ape_loader_dd_count,
#if SupportsMetal()
HIDDEN(v_ape_realsectors =
MIN(0x70000 - IMAGE_BASE_REAL,
ROUNDUP(RVA(_edata), 512)) / 512);
ROUNDUP(RVA(_tdata_end), 512)) / 512);
HIDDEN(v_ape_realpages = v_ape_realsectors / (4096 / 512));
HIDDEN(v_ape_highsectors =
(ROUNDUP(RVA(_edata), 512) / 512) - v_ape_realsectors);
(ROUNDUP(RVA(_tdata_end), 512) / 512) - v_ape_realsectors);
TSSDESCSTUB2(_tss, _tss, _tss_end ? _tss_end - _tss - 1 : 0);
#endif

View file

@ -10,6 +10,10 @@ extern unsigned char _edata[];
extern unsigned char _ezip[];
extern unsigned char _end[];
extern unsigned char _ereal[];
extern unsigned char _tdata_start[];
extern unsigned char _tdata_end[];
extern unsigned char _tbss_start[];
extern unsigned char _tbss_end[];
extern unsigned char __privileged_start[];
extern unsigned char __privileged_addr[];
extern unsigned char __privileged_size[];

View file

@ -18,6 +18,7 @@
*/
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/metalfile.internal.h"
#include "libc/calls/struct/metastat.internal.h"
#include "libc/calls/struct/stat.h"
#include "libc/calls/syscall-sysv.internal.h"
@ -60,7 +61,14 @@ ssize_t getfiledescriptorsize(int fd) {
rc = -1;
}
} else if (IsMetal()) {
rc = -1;
if (fd < 0 || fd >= g_fds.n) {
rc = ebadf();
} else if (g_fds.p[fd].kind != kFdFile) {
rc = eacces();
} else {
struct MetalFile *state = (struct MetalFile *)g_fds.p[fd].handle;
rc = state->size;
}
} else if (!IsWindows()) {
if (!__sys_fstat(fd, &st)) {
rc = METASTAT(st, st_size);

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/metalfile.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
@ -76,6 +77,11 @@ static inline void GetProgramExecutableNameImpl(char *p, char *e) {
return;
}
if (IsMetal()) {
if (!memccpy(p, APE_COM_NAME, 0, e - p - 1)) e[-1] = 0;
return;
}
// if argv[0] exists then turn it into an absolute path. we also try
// adding a .com suffix since the ape auto-appends it when resolving
if (__argc && (((q = __argv[0]) && !sys_faccessat(AT_FDCWD, q, F_OK, 0)) ||

75
libc/calls/metalfile.c Normal file
View file

@ -0,0 +1,75 @@
/*-*- 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 "ape/relocations.h"
#include "ape/sections.internal.h"
#include "libc/assert.h"
#include "libc/calls/internal.h"
#include "libc/calls/metalfile.internal.h"
#include "libc/intrin/directmap.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/pc.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/errfuns.h"
#define MAP_ANONYMOUS_linux 0x00000020
#define MAP_FIXED_linux 0x00000010
#define MAP_SHARED_linux 0x00000001
STATIC_YOINK("_init_metalfile");
void *__ape_com_base;
size_t __ape_com_size = 0;
textstartup void InitializeMetalFile(void) {
if (IsMetal()) {
/*
* Copy out a pristine image of the program before the program might
* decide to modify its own .data section.
*
* This code is included if a symbol "file:/proc/self/exe" is defined
* (see libc/calls/metalfile.internal.h & libc/calls/metalfile_init.S).
* The zipos code will automatically arrange to do this. Alternatively,
* user code can STATIC_YOINK this symbol.
*/
size_t size = ROUNDUP(_tdata_end - _base, 4096);
void *copied_base;
struct DirectMap dm;
dm = sys_mmap_metal(NULL, size, PROT_READ | PROT_WRITE,
MAP_SHARED_linux | MAP_ANONYMOUS_linux, -1, 0);
copied_base = dm.addr;
_npassert(copied_base != (void *)-1);
memcpy(copied_base, (void *)(BANE + IMAGE_BASE_REAL), size);
__ape_com_base = copied_base;
__ape_com_size = size;
}
}

View file

@ -9,6 +9,12 @@ struct MetalFile {
size_t pos;
};
extern void *__ape_com_base;
extern size_t __ape_com_size;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#define APE_COM_NAME "/proc/self/exe"
#endif /* COSMOPOLITAN_LIBC_CALLS_METALFILE_INTERNAL_H_ */

View file

@ -0,0 +1,38 @@
/*-*- 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"
#include "libc/calls/metalfile.internal.h"
.init.start 101,_init_metalfile
push %rdi
push %rsi
call InitializeMetalFile
pop %rsi
pop %rdi
.init.end 101,_init_metalfile
APE_COM_NAME:
.endobj APE_COM_NAME,globl,hidden

View file

@ -26,8 +26,8 @@ noasan int sys_munmap_metal(void *addr, size_t size) {
mm = (struct mman *)(BANE + 0x0500);
for (i = 0; i < size; i += 4096) {
e = __get_virtual(mm, __get_pml4t(), (uint64_t)addr + i, false);
if (e) *e = ~PAGE_V;
invlpg(e);
if (e) *e = ~(PAGE_V | PAGE_RSRV);
invlpg((uint64_t)addr + i);
}
return 0;
}

View file

@ -16,24 +16,46 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "ape/relocations.h"
#include "ape/sections.internal.h"
#include "libc/assert.h"
#include "libc/calls/internal.h"
#include "libc/calls/metalfile.internal.h"
#include "libc/intrin/directmap.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/pc.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/errfuns.h"
#include "libc/zipos/zipos.internal.h"
int sys_openat_metal(int dirfd, const char *file, int flags, unsigned mode) {
int fd;
struct MetalFile *state;
if (strcmp(file, "ape.com")) return enoent();
if (!_weaken(calloc)) return enomem();
if (dirfd != AT_FDCWD || strcmp(file, APE_COM_NAME)) return enoent();
if (flags != O_RDONLY) return eacces();
if (!_weaken(__ape_com_base) || !_weaken(__ape_com_size))
return eopnotsupp();
if ((fd = __reservefd(-1)) == -1) return -1;
state = _weaken(calloc)(1, sizeof(struct MetalFile));
state->base = (char *)_base;
state->size = _end - _base;
if (!_weaken(calloc) || !_weaken(free)) {
struct DirectMap dm;
dm = sys_mmap_metal(NULL, ROUNDUP(sizeof(struct MetalFile), 4096),
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS,
-1, 0);
state = dm.addr;
if (state == (void *)-1) return -1;
} else {
state = _weaken(calloc)(1, sizeof(struct MetalFile));
if (!state) return -1;
}
state->base = (char *)__ape_com_base;
state->size = __ape_com_size;
g_fds.p[fd].kind = kFdFile;
g_fds.p[fd].flags = flags;
g_fds.p[fd].mode = mode;

View file

@ -18,28 +18,56 @@
*/
#include "libc/calls/calls.h"
#include "libc/intrin/directmap.internal.h"
#include "libc/calls/internal.h"
#include "libc/calls/metalfile.internal.h"
#include "libc/macros.internal.h"
#include "libc/runtime/pc.internal.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/errfuns.h"
#define MAP_ANONYMOUS_linux 0x00000020
#define MAP_FIXED_linux 0x00000010
static uint64_t sys_mmap_metal_break;
noasan struct DirectMap sys_mmap_metal(void *paddr, size_t size, int prot,
noasan static struct DirectMap bad_mmap(void) {
struct DirectMap res;
res.addr = (void *)-1;
res.maphandle = -1;
return res;
}
noasan struct DirectMap sys_mmap_metal(void *vaddr, size_t size, int prot,
int flags, int fd, int64_t off) {
/* asan runtime depends on this function */
size_t i;
struct mman *mm;
struct DirectMap res;
uint64_t addr, page, *pte, *pml4t;
uint64_t addr, faddr = 0, page, *pte, *fdpte, *pml4t;
mm = (struct mman *)(BANE + 0x0500);
pml4t = __get_pml4t();
size = ROUNDUP(size, 4096);
addr = (uint64_t)paddr;
if (!(flags & MAP_FIXED)) {
if (!addr) addr = 4096;
addr = (uint64_t)vaddr;
if (!(flags & MAP_ANONYMOUS_linux)) {
struct Fd *sfd;
struct MetalFile *file;
if (off < 0 || fd < 0 || fd >= g_fds.n) return bad_mmap();
sfd = &g_fds.p[fd];
if (sfd->kind != kFdFile) return bad_mmap();
file = (struct MetalFile *)sfd->handle;
/* TODO: allow mapping partial page at end of file, if file size not
* multiple of page size */
if (off > file->size || size > file->size - off) return bad_mmap();
faddr = (uint64_t)file->base + off;
if (faddr % 4096 != 0) return bad_mmap();
}
if (!(flags & MAP_FIXED_linux)) {
if (!addr) {
addr = 4096;
} else {
addr = ROUNDUP(addr, 4096);
}
for (i = 0; i < size; i += 4096) {
pte = __get_virtual(mm, pml4t, addr + i, false);
if (pte && (*pte & (PAGE_V | PAGE_RSRV))) {
@ -52,15 +80,26 @@ noasan struct DirectMap sys_mmap_metal(void *paddr, size_t size, int prot,
for (i = 0; i < size; i += 4096) {
page = __new_page(mm);
pte = __get_virtual(mm, pml4t, addr + i, true);
if (pte && page) {
__clear_page(BANE + page);
page |= PAGE_RSRV | PAGE_U;
if ((prot & PROT_WRITE))
page |= PAGE_V | PAGE_RW;
else if ((prot & (PROT_READ | PROT_EXEC)))
page |= PAGE_V;
if (!(prot & PROT_EXEC)) page |= PAGE_XD;
if (pte) {
if ((flags & MAP_ANONYMOUS_linux)) {
page = __new_page(mm);
if (!page) return bad_mmap();
__clear_page(BANE + page);
page |= PAGE_RSRV | PAGE_U;
if ((prot & PROT_WRITE))
page |= PAGE_V | PAGE_RW;
else if ((prot & (PROT_READ | PROT_EXEC)))
page |= PAGE_V;
if (!(prot & PROT_EXEC)) page |= PAGE_XD;
} else {
fdpte = __get_virtual(mm, pml4t, faddr + i, false);
page = *fdpte;
page |= PAGE_RSRV | PAGE_U;
if (!(prot & PROT_WRITE)) page &= ~PAGE_RW;
if (!(prot & PROT_EXEC)) page |= PAGE_XD;
}
*pte = page;
invlpg(addr + i);
} else {
addr = -1;
break;

View file

@ -40,7 +40,7 @@ struct mman {
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
/* Values for mman::pc_video_type. TODO: implement graphics modes. */
/* Values for mman::pc_video_type. */
#define PC_VIDEO_TEXT 0
#define PC_VIDEO_BGR565 1
#define PC_VIDEO_BGR555 2

View file

@ -145,15 +145,15 @@
(If this maps 2MB/1GB page and CR4.PGE) Global
(If IsPage 2MB/1GB, see Intel V3A § 11.12) PAT
Must Be 0 Next Page Table Address (!IsPage)
Physical Address 4KB
ign
PKE ign Physical Address 2MB
Phys. Addr. 1GB
Must Be 0 Next Page Table Address (!IsPage)
Physical Address 4KB
ign
PK ign Physical Address 2MB
Phys. Addr. 1GB
6666555555555544444444443333333333222222222211111111110000000000
3210987654321098765432109876543210987654321098765432109876543210*/
#define PAGE_V /* */ 0b000000000001
@ -163,7 +163,7 @@
#define PAGE_2MB /* */ 0b000110000000
#define PAGE_1GB /* */ 0b000110000000
#define PAGE_IGN1 /* */ 0b111000000000
#define PAGE_RSRV /* blinkenlights reservation */ 0b001000000000
#define PAGE_RSRV /* blinkenlights/libc reservation */ 0b001000000000
#define PAGE_GROD /* blinkenlights MAP_GROWSDOWN */ 0b010000000000
#define PAGE_TA 0x00007ffffffff000
#define PAGE_PA2 0x00007fffffe00000

View file

@ -41,6 +41,7 @@ void *GetZipCdir(const uint8_t *p, size_t n) {
size_t i, j;
v8hi pk = {READ16LE("PK"), READ16LE("PK"), READ16LE("PK"), READ16LE("PK"),
READ16LE("PK"), READ16LE("PK"), READ16LE("PK"), READ16LE("PK")};
uint32_t magic;
i = n - 4;
asm("" : "+x"(pk));
do {
@ -54,11 +55,14 @@ void *GetZipCdir(const uint8_t *p, size_t n) {
continue;
}
}
if (READ32LE(p + i) == kZipCdir64LocatorMagic &&
while (magic = READ32LE(p + i),
magic != kZipCdir64LocatorMagic && magic != kZipCdirHdrMagic &&
i + 0x10000 + 0x1000 >= n) --i;
if (magic == kZipCdir64LocatorMagic &&
i + kZipCdir64LocatorSize <= n &&
IsZipCdir64(p, n, ZIP_LOCATE64_OFFSET(p + i))) {
return p + ZIP_LOCATE64_OFFSET(p + i);
} else if (READ32LE(p + i) == kZipCdirHdrMagic && IsZipCdir32(p, n, i)) {
} else if (magic == kZipCdirHdrMagic && IsZipCdir32(p, n, i)) {
j = i;
do {
if (READ32LE(p + j) == kZipCdir64LocatorMagic &&
@ -69,6 +73,6 @@ void *GetZipCdir(const uint8_t *p, size_t n) {
} while (j-- && i - j < 128);
return p + i;
}
} while (i--);
} while (i-- + 0x10000 + 0x1000 >= n);
return 0;
}

View file

@ -629,3 +629,5 @@ str.mode_list_start:
.byte PC_VIDEO_BGRX8888,32, 8,16, 8, 8, 8, 0
.endobj .type_list
.type_list_end:
.previous

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/metalfile.internal.h"
#include "libc/intrin/cmpxchg.h"
#include "libc/intrin/promises.internal.h"
#include "libc/intrin/strace.internal.h"
@ -29,6 +30,8 @@
#include "libc/zip.h"
#include "libc/zipos/zipos.internal.h"
STATIC_YOINK(APE_COM_NAME);
static uint64_t __zipos_get_min_offset(const uint8_t *base,
const uint8_t *cdir) {
uint64_t i, n, c, r, o;

View file

@ -165,7 +165,7 @@ static int __zipos_load(struct Zipos *zipos, size_t cf, unsigned flags,
minfd = 3;
__fds_lock();
TryAgain:
if (IsWindows()) {
if (IsWindows() || IsMetal()) {
if ((fd = __reservefd_unlocked(-1)) != -1) {
return __zipos_setfd(fd, h, flags, mode);
}