mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-03-03 15:38:22 +00:00
Work towards zipos / open(argv[0]) on metal (#667)
This commit is contained in:
parent
543c93f623
commit
cb9a0466f1
17 changed files with 257 additions and 46 deletions
18
ape/ape.S
18
ape/ape.S
|
@ -35,6 +35,7 @@
|
||||||
#include "ape/macros.internal.h"
|
#include "ape/macros.internal.h"
|
||||||
#include "ape/notice.inc"
|
#include "ape/notice.inc"
|
||||||
#include "ape/relocations.h"
|
#include "ape/relocations.h"
|
||||||
|
#include "libc/calls/metalfile.internal.h"
|
||||||
#include "libc/dce.h"
|
#include "libc/dce.h"
|
||||||
#include "libc/elf/def.h"
|
#include "libc/elf/def.h"
|
||||||
#include "libc/macho.internal.h"
|
#include "libc/macho.internal.h"
|
||||||
|
@ -1565,19 +1566,19 @@ kernel: movabs $ape_stack_vaddr,%rsp
|
||||||
#endif
|
#endif
|
||||||
push $_HOSTMETAL # sets __hostos in crt.S
|
push $_HOSTMETAL # sets __hostos in crt.S
|
||||||
pop %rcx
|
pop %rcx
|
||||||
push $0
|
pushq .Lenv0(%rip) # envp[0][0]
|
||||||
mov %rsp,%rbp
|
mov %rsp,%rbp
|
||||||
mov .Lenv0(%rip),%rax
|
pushq .Largv0+8(%rip) # argv[0][8]
|
||||||
mov %rax,(%rbp) # envp[0][0]
|
pushq .Largv0(%rip) # argv[0][0]
|
||||||
push $0 # argv[0][0]
|
mov %rsp,%rax
|
||||||
push $0 # auxv[1][1]
|
push $0 # auxv[1][1]
|
||||||
push $0 # auxv[1][0]
|
push $0 # auxv[1][0]
|
||||||
push %rbp # auxv[0][1]
|
push %rax # auxv[0][1]
|
||||||
push $31 # auxv[0][0] AT_EXECFN
|
push $31 # auxv[0][0] AT_EXECFN
|
||||||
push $0 # envp[1]
|
push $0 # envp[1]
|
||||||
push $.Lenv0 # envp[0]
|
push %rbp # envp[0]
|
||||||
push $0 # argv[1]
|
push $0 # argv[1]
|
||||||
push %rbp # argv[0]
|
push %rax # argv[0]
|
||||||
push $1 # argc
|
push $1 # argc
|
||||||
xor %ebp,%ebp
|
xor %ebp,%ebp
|
||||||
xor %eax,%eax
|
xor %eax,%eax
|
||||||
|
@ -1593,6 +1594,9 @@ kernel: movabs $ape_stack_vaddr,%rsp
|
||||||
|
|
||||||
.rodata
|
.rodata
|
||||||
.Lenv0: .asciz "METAL=1"
|
.Lenv0: .asciz "METAL=1"
|
||||||
|
.Largv0:
|
||||||
|
.asciz APE_COM_NAME
|
||||||
|
.org .Largv0+16
|
||||||
.previous
|
.previous
|
||||||
|
|
||||||
#endif /* SupportsMetal() */
|
#endif /* SupportsMetal() */
|
||||||
|
|
|
@ -565,10 +565,10 @@ SHSTUB2(ape_loader_dd_count,
|
||||||
#if SupportsMetal()
|
#if SupportsMetal()
|
||||||
HIDDEN(v_ape_realsectors =
|
HIDDEN(v_ape_realsectors =
|
||||||
MIN(0x70000 - IMAGE_BASE_REAL,
|
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_realpages = v_ape_realsectors / (4096 / 512));
|
||||||
HIDDEN(v_ape_highsectors =
|
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);
|
TSSDESCSTUB2(_tss, _tss, _tss_end ? _tss_end - _tss - 1 : 0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,10 @@ extern unsigned char _edata[];
|
||||||
extern unsigned char _ezip[];
|
extern unsigned char _ezip[];
|
||||||
extern unsigned char _end[];
|
extern unsigned char _end[];
|
||||||
extern unsigned char _ereal[];
|
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_start[];
|
||||||
extern unsigned char __privileged_addr[];
|
extern unsigned char __privileged_addr[];
|
||||||
extern unsigned char __privileged_size[];
|
extern unsigned char __privileged_size[];
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/calls/calls.h"
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/calls/internal.h"
|
#include "libc/calls/internal.h"
|
||||||
|
#include "libc/calls/metalfile.internal.h"
|
||||||
#include "libc/calls/struct/metastat.internal.h"
|
#include "libc/calls/struct/metastat.internal.h"
|
||||||
#include "libc/calls/struct/stat.h"
|
#include "libc/calls/struct/stat.h"
|
||||||
#include "libc/calls/syscall-sysv.internal.h"
|
#include "libc/calls/syscall-sysv.internal.h"
|
||||||
|
@ -60,7 +61,14 @@ ssize_t getfiledescriptorsize(int fd) {
|
||||||
rc = -1;
|
rc = -1;
|
||||||
}
|
}
|
||||||
} else if (IsMetal()) {
|
} 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()) {
|
} else if (!IsWindows()) {
|
||||||
if (!__sys_fstat(fd, &st)) {
|
if (!__sys_fstat(fd, &st)) {
|
||||||
rc = METASTAT(st, st_size);
|
rc = METASTAT(st, st_size);
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/calls/calls.h"
|
#include "libc/calls/calls.h"
|
||||||
|
#include "libc/calls/metalfile.internal.h"
|
||||||
#include "libc/calls/syscall-sysv.internal.h"
|
#include "libc/calls/syscall-sysv.internal.h"
|
||||||
#include "libc/dce.h"
|
#include "libc/dce.h"
|
||||||
#include "libc/errno.h"
|
#include "libc/errno.h"
|
||||||
|
@ -76,6 +77,11 @@ static inline void GetProgramExecutableNameImpl(char *p, char *e) {
|
||||||
return;
|
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
|
// 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
|
// 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)) ||
|
if (__argc && (((q = __argv[0]) && !sys_faccessat(AT_FDCWD, q, F_OK, 0)) ||
|
||||||
|
|
75
libc/calls/metalfile.c
Normal file
75
libc/calls/metalfile.c
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,6 +9,12 @@ struct MetalFile {
|
||||||
size_t pos;
|
size_t pos;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
extern void *__ape_com_base;
|
||||||
|
extern size_t __ape_com_size;
|
||||||
|
|
||||||
COSMOPOLITAN_C_END_
|
COSMOPOLITAN_C_END_
|
||||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||||
|
|
||||||
|
#define APE_COM_NAME "/proc/self/exe"
|
||||||
|
|
||||||
#endif /* COSMOPOLITAN_LIBC_CALLS_METALFILE_INTERNAL_H_ */
|
#endif /* COSMOPOLITAN_LIBC_CALLS_METALFILE_INTERNAL_H_ */
|
||||||
|
|
38
libc/calls/metalfile_init.S
Normal file
38
libc/calls/metalfile_init.S
Normal 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
|
|
@ -26,8 +26,8 @@ noasan int sys_munmap_metal(void *addr, size_t size) {
|
||||||
mm = (struct mman *)(BANE + 0x0500);
|
mm = (struct mman *)(BANE + 0x0500);
|
||||||
for (i = 0; i < size; i += 4096) {
|
for (i = 0; i < size; i += 4096) {
|
||||||
e = __get_virtual(mm, __get_pml4t(), (uint64_t)addr + i, false);
|
e = __get_virtual(mm, __get_pml4t(), (uint64_t)addr + i, false);
|
||||||
if (e) *e = ~PAGE_V;
|
if (e) *e = ~(PAGE_V | PAGE_RSRV);
|
||||||
invlpg(e);
|
invlpg((uint64_t)addr + i);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,24 +16,46 @@
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "ape/relocations.h"
|
||||||
#include "ape/sections.internal.h"
|
#include "ape/sections.internal.h"
|
||||||
|
#include "libc/assert.h"
|
||||||
#include "libc/calls/internal.h"
|
#include "libc/calls/internal.h"
|
||||||
#include "libc/calls/metalfile.internal.h"
|
#include "libc/calls/metalfile.internal.h"
|
||||||
|
#include "libc/intrin/directmap.internal.h"
|
||||||
#include "libc/intrin/weaken.h"
|
#include "libc/intrin/weaken.h"
|
||||||
|
#include "libc/macros.internal.h"
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
|
#include "libc/runtime/pc.internal.h"
|
||||||
#include "libc/runtime/runtime.h"
|
#include "libc/runtime/runtime.h"
|
||||||
#include "libc/str/str.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/sysv/errfuns.h"
|
||||||
|
#include "libc/zipos/zipos.internal.h"
|
||||||
|
|
||||||
int sys_openat_metal(int dirfd, const char *file, int flags, unsigned mode) {
|
int sys_openat_metal(int dirfd, const char *file, int flags, unsigned mode) {
|
||||||
int fd;
|
int fd;
|
||||||
struct MetalFile *state;
|
struct MetalFile *state;
|
||||||
if (strcmp(file, "ape.com")) return enoent();
|
if (dirfd != AT_FDCWD || strcmp(file, APE_COM_NAME)) return enoent();
|
||||||
if (!_weaken(calloc)) return enomem();
|
if (flags != O_RDONLY) return eacces();
|
||||||
|
if (!_weaken(__ape_com_base) || !_weaken(__ape_com_size))
|
||||||
|
return eopnotsupp();
|
||||||
if ((fd = __reservefd(-1)) == -1) return -1;
|
if ((fd = __reservefd(-1)) == -1) return -1;
|
||||||
|
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));
|
state = _weaken(calloc)(1, sizeof(struct MetalFile));
|
||||||
state->base = (char *)_base;
|
if (!state) return -1;
|
||||||
state->size = _end - _base;
|
}
|
||||||
|
state->base = (char *)__ape_com_base;
|
||||||
|
state->size = __ape_com_size;
|
||||||
g_fds.p[fd].kind = kFdFile;
|
g_fds.p[fd].kind = kFdFile;
|
||||||
g_fds.p[fd].flags = flags;
|
g_fds.p[fd].flags = flags;
|
||||||
g_fds.p[fd].mode = mode;
|
g_fds.p[fd].mode = mode;
|
||||||
|
|
|
@ -18,28 +18,56 @@
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/calls/calls.h"
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/intrin/directmap.internal.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/macros.internal.h"
|
||||||
#include "libc/runtime/pc.internal.h"
|
#include "libc/runtime/pc.internal.h"
|
||||||
#include "libc/str/str.h"
|
#include "libc/str/str.h"
|
||||||
#include "libc/sysv/consts/map.h"
|
|
||||||
#include "libc/sysv/consts/prot.h"
|
#include "libc/sysv/consts/prot.h"
|
||||||
#include "libc/sysv/errfuns.h"
|
#include "libc/sysv/errfuns.h"
|
||||||
|
|
||||||
|
#define MAP_ANONYMOUS_linux 0x00000020
|
||||||
|
#define MAP_FIXED_linux 0x00000010
|
||||||
|
|
||||||
static uint64_t sys_mmap_metal_break;
|
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) {
|
int flags, int fd, int64_t off) {
|
||||||
/* asan runtime depends on this function */
|
/* asan runtime depends on this function */
|
||||||
size_t i;
|
size_t i;
|
||||||
struct mman *mm;
|
struct mman *mm;
|
||||||
struct DirectMap res;
|
struct DirectMap res;
|
||||||
uint64_t addr, page, *pte, *pml4t;
|
uint64_t addr, faddr = 0, page, *pte, *fdpte, *pml4t;
|
||||||
mm = (struct mman *)(BANE + 0x0500);
|
mm = (struct mman *)(BANE + 0x0500);
|
||||||
pml4t = __get_pml4t();
|
pml4t = __get_pml4t();
|
||||||
size = ROUNDUP(size, 4096);
|
size = ROUNDUP(size, 4096);
|
||||||
addr = (uint64_t)paddr;
|
addr = (uint64_t)vaddr;
|
||||||
if (!(flags & MAP_FIXED)) {
|
if (!(flags & MAP_ANONYMOUS_linux)) {
|
||||||
if (!addr) addr = 4096;
|
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) {
|
for (i = 0; i < size; i += 4096) {
|
||||||
pte = __get_virtual(mm, pml4t, addr + i, false);
|
pte = __get_virtual(mm, pml4t, addr + i, false);
|
||||||
if (pte && (*pte & (PAGE_V | PAGE_RSRV))) {
|
if (pte && (*pte & (PAGE_V | PAGE_RSRV))) {
|
||||||
|
@ -52,7 +80,10 @@ noasan struct DirectMap sys_mmap_metal(void *paddr, size_t size, int prot,
|
||||||
for (i = 0; i < size; i += 4096) {
|
for (i = 0; i < size; i += 4096) {
|
||||||
page = __new_page(mm);
|
page = __new_page(mm);
|
||||||
pte = __get_virtual(mm, pml4t, addr + i, true);
|
pte = __get_virtual(mm, pml4t, addr + i, true);
|
||||||
if (pte && page) {
|
if (pte) {
|
||||||
|
if ((flags & MAP_ANONYMOUS_linux)) {
|
||||||
|
page = __new_page(mm);
|
||||||
|
if (!page) return bad_mmap();
|
||||||
__clear_page(BANE + page);
|
__clear_page(BANE + page);
|
||||||
page |= PAGE_RSRV | PAGE_U;
|
page |= PAGE_RSRV | PAGE_U;
|
||||||
if ((prot & PROT_WRITE))
|
if ((prot & PROT_WRITE))
|
||||||
|
@ -60,7 +91,15 @@ noasan struct DirectMap sys_mmap_metal(void *paddr, size_t size, int prot,
|
||||||
else if ((prot & (PROT_READ | PROT_EXEC)))
|
else if ((prot & (PROT_READ | PROT_EXEC)))
|
||||||
page |= PAGE_V;
|
page |= PAGE_V;
|
||||||
if (!(prot & PROT_EXEC)) page |= PAGE_XD;
|
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;
|
*pte = page;
|
||||||
|
invlpg(addr + i);
|
||||||
} else {
|
} else {
|
||||||
addr = -1;
|
addr = -1;
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -40,7 +40,7 @@ struct mman {
|
||||||
COSMOPOLITAN_C_END_
|
COSMOPOLITAN_C_END_
|
||||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
#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_TEXT 0
|
||||||
#define PC_VIDEO_BGR565 1
|
#define PC_VIDEO_BGR565 1
|
||||||
#define PC_VIDEO_BGR555 2
|
#define PC_VIDEO_BGR555 2
|
||||||
|
|
|
@ -145,13 +145,13 @@
|
||||||
│ (If this maps 2MB/1GB page and CR4.PGE) Global┐││││││││
|
│ (If this maps 2MB/1GB page and CR4.PGE) Global┐││││││││
|
||||||
│ (If IsPage 2MB/1GB, see Intel V3A § 11.12) PAT │││││││││
|
│ (If IsPage 2MB/1GB, see Intel V3A § 11.12) PAT │││││││││
|
||||||
│ │ │││││││││
|
│ │ │││││││││
|
||||||
│ ┌────────────────────────────────────┤ │││││││││
|
│ ┌─────────────────────────────────────┤ │││││││││
|
||||||
│ Must Be 0┐│ Next Page Table Address (!IsPage) │ │││││││││
|
│ Must Be 0┐│ Next Page Table Address (!IsPage) │ │││││││││
|
||||||
│ │├────────────────────────────────────┤ │││││││││
|
│ │├─────────────────────────────────────┤ │││││││││
|
||||||
│ ││ Physical Address 4KB │ │││││││││
|
│ ││ Physical Address 4KB │ │││││││││
|
||||||
│┌───┐┌─────┐│├───────────────────────────┐ │ign│││││││││
|
│┌──┐┌─────┐│├────────────────────────────┐ │ign│││││││││
|
||||||
││PKE││ ign │││ Physical Address 2MB │ │┌┴┐│││││││││
|
││PK││ ign │││ Physical Address 2MB │ │┌┴┐│││││││││
|
||||||
││ ││ ││├──────────────────┐ │ ││ ││││││││││
|
││ ││ ││├───────────────────┐ │ ││ ││││││││││
|
||||||
││ ││ │││ Phys. Addr. 1GB │ │ ││ ││││││││││
|
││ ││ │││ Phys. Addr. 1GB │ │ ││ ││││││││││
|
||||||
││ ││ │││ │ │ ││ ││││││││││
|
││ ││ │││ │ │ ││ ││││││││││
|
||||||
6666555555555544444444443333333333222222222211111111110000000000
|
6666555555555544444444443333333333222222222211111111110000000000
|
||||||
|
@ -163,7 +163,7 @@
|
||||||
#define PAGE_2MB /* */ 0b000110000000
|
#define PAGE_2MB /* */ 0b000110000000
|
||||||
#define PAGE_1GB /* */ 0b000110000000
|
#define PAGE_1GB /* */ 0b000110000000
|
||||||
#define PAGE_IGN1 /* */ 0b111000000000
|
#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_GROD /* blinkenlights MAP_GROWSDOWN */ 0b010000000000
|
||||||
#define PAGE_TA 0x00007ffffffff000
|
#define PAGE_TA 0x00007ffffffff000
|
||||||
#define PAGE_PA2 0x00007fffffe00000
|
#define PAGE_PA2 0x00007fffffe00000
|
||||||
|
|
|
@ -41,6 +41,7 @@ void *GetZipCdir(const uint8_t *p, size_t n) {
|
||||||
size_t i, j;
|
size_t i, j;
|
||||||
v8hi pk = {READ16LE("PK"), READ16LE("PK"), READ16LE("PK"), READ16LE("PK"),
|
v8hi pk = {READ16LE("PK"), READ16LE("PK"), READ16LE("PK"), READ16LE("PK"),
|
||||||
READ16LE("PK"), READ16LE("PK"), READ16LE("PK"), READ16LE("PK")};
|
READ16LE("PK"), READ16LE("PK"), READ16LE("PK"), READ16LE("PK")};
|
||||||
|
uint32_t magic;
|
||||||
i = n - 4;
|
i = n - 4;
|
||||||
asm("" : "+x"(pk));
|
asm("" : "+x"(pk));
|
||||||
do {
|
do {
|
||||||
|
@ -54,11 +55,14 @@ void *GetZipCdir(const uint8_t *p, size_t n) {
|
||||||
continue;
|
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 &&
|
i + kZipCdir64LocatorSize <= n &&
|
||||||
IsZipCdir64(p, n, ZIP_LOCATE64_OFFSET(p + i))) {
|
IsZipCdir64(p, n, ZIP_LOCATE64_OFFSET(p + i))) {
|
||||||
return p + 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;
|
j = i;
|
||||||
do {
|
do {
|
||||||
if (READ32LE(p + j) == kZipCdir64LocatorMagic &&
|
if (READ32LE(p + j) == kZipCdir64LocatorMagic &&
|
||||||
|
@ -69,6 +73,6 @@ void *GetZipCdir(const uint8_t *p, size_t n) {
|
||||||
} while (j-- && i - j < 128);
|
} while (j-- && i - j < 128);
|
||||||
return p + i;
|
return p + i;
|
||||||
}
|
}
|
||||||
} while (i--);
|
} while (i-- + 0x10000 + 0x1000 >= n);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -629,3 +629,5 @@ str.mode_list_start:
|
||||||
.byte PC_VIDEO_BGRX8888,32, 8,16, 8, 8, 8, 0
|
.byte PC_VIDEO_BGRX8888,32, 8,16, 8, 8, 8, 0
|
||||||
.endobj .type_list
|
.endobj .type_list
|
||||||
.type_list_end:
|
.type_list_end:
|
||||||
|
|
||||||
|
.previous
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/calls/calls.h"
|
#include "libc/calls/calls.h"
|
||||||
|
#include "libc/calls/metalfile.internal.h"
|
||||||
#include "libc/intrin/cmpxchg.h"
|
#include "libc/intrin/cmpxchg.h"
|
||||||
#include "libc/intrin/promises.internal.h"
|
#include "libc/intrin/promises.internal.h"
|
||||||
#include "libc/intrin/strace.internal.h"
|
#include "libc/intrin/strace.internal.h"
|
||||||
|
@ -29,6 +30,8 @@
|
||||||
#include "libc/zip.h"
|
#include "libc/zip.h"
|
||||||
#include "libc/zipos/zipos.internal.h"
|
#include "libc/zipos/zipos.internal.h"
|
||||||
|
|
||||||
|
STATIC_YOINK(APE_COM_NAME);
|
||||||
|
|
||||||
static uint64_t __zipos_get_min_offset(const uint8_t *base,
|
static uint64_t __zipos_get_min_offset(const uint8_t *base,
|
||||||
const uint8_t *cdir) {
|
const uint8_t *cdir) {
|
||||||
uint64_t i, n, c, r, o;
|
uint64_t i, n, c, r, o;
|
||||||
|
|
|
@ -165,7 +165,7 @@ static int __zipos_load(struct Zipos *zipos, size_t cf, unsigned flags,
|
||||||
minfd = 3;
|
minfd = 3;
|
||||||
__fds_lock();
|
__fds_lock();
|
||||||
TryAgain:
|
TryAgain:
|
||||||
if (IsWindows()) {
|
if (IsWindows() || IsMetal()) {
|
||||||
if ((fd = __reservefd_unlocked(-1)) != -1) {
|
if ((fd = __reservefd_unlocked(-1)) != -1) {
|
||||||
return __zipos_setfd(fd, h, flags, mode);
|
return __zipos_setfd(fd, h, flags, mode);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue