lib: c: re-organise into separate static modules, plus a single shared library
This commit is contained in:
33
lib/libc/io/CMakeLists.txt
Normal file
33
lib/libc/io/CMakeLists.txt
Normal file
@@ -0,0 +1,33 @@
|
||||
set(source_dirs unistd stdio)
|
||||
|
||||
file(GLOB sources *.c *.h)
|
||||
|
||||
foreach (dir ${source_dirs})
|
||||
file(GLOB dir_sources ${CMAKE_CURRENT_SOURCE_DIR}/${dir}/*.c)
|
||||
file(GLOB dir_headers ${CMAKE_CURRENT_SOURCE_DIR}/${dir}/*.h)
|
||||
|
||||
set(sources ${sources} ${dir_sources})
|
||||
set(headers ${headers} ${dir_headers})
|
||||
endforeach (dir)
|
||||
|
||||
file(GLOB_RECURSE sub_headers ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)
|
||||
set(headers ${headers} ${sub_headers})
|
||||
|
||||
set(component_sources ${sources} PARENT_SCOPE)
|
||||
set(component_headers ${headers} PARENT_SCOPE)
|
||||
set(component_public_include_dirs ${CMAKE_CURRENT_SOURCE_DIR}/include PARENT_SCOPE)
|
||||
|
||||
rosetta_add_library(STATIC
|
||||
NAME libc-io
|
||||
PUBLIC_INCLUDE_DIRS
|
||||
${public_include_dirs}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||
SOURCES ${sources}
|
||||
HEADERS ${headers})
|
||||
|
||||
sysroot_add_library(
|
||||
NAME libc-io
|
||||
HEADER_DIR /usr/include
|
||||
LIB_DIR /usr/lib)
|
||||
|
||||
target_link_libraries(libc-io libc-core interface::fs libxpc-static libmango)
|
||||
2
lib/libc/io/fs.c
Normal file
2
lib/libc/io/fs.c
Normal file
@@ -0,0 +1,2 @@
|
||||
#define MSG_IMPLEMENTATION
|
||||
#include <rosetta/fs.h>
|
||||
43
lib/libc/io/include/sys/mman.h
Normal file
43
lib/libc/io/include/sys/mman.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef SYS_MMAN_H_
|
||||
#define SYS_MMAN_H_
|
||||
|
||||
#include <mango/types.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define PROT_NONE 0x00u
|
||||
#define PROT_EXEC 0x01u
|
||||
#define PROT_READ 0x02u
|
||||
#define PROT_WRITE 0x04u
|
||||
|
||||
#define MAP_SHARED 0x01u
|
||||
#define MAP_SHARED_VALIDATE 0x03u
|
||||
#define MAP_PRIVATE 0x04u
|
||||
#define MAP_32BIT 0x08u
|
||||
#define MAP_ANON MAP_ANONYMOUS
|
||||
#define MAP_ANONYMOUS 0x10u
|
||||
#define MAP_DENYWRITE 0x20u
|
||||
#define MAP_EXECUTABLE 0x40u
|
||||
#define MAP_FILE 0x80u
|
||||
#define MAP_FIXED 0x100u
|
||||
#define MAP_FIXED_NOREPLACE 0x300u
|
||||
#define MAP_GROWSDOWN 0x400u
|
||||
#define MAP_HUGETLB 0x800u
|
||||
#define MAP_HUGE_2MB 0x1000u
|
||||
#define MAP_HUGE_1GB 0x2000u
|
||||
#define MAP_LOCKED 0x4000u
|
||||
#define MAP_NONBLOCK 0x8000u
|
||||
#define MAP_NORESERVE 0x10000u
|
||||
#define MAP_POPULATE 0x20000u
|
||||
#define MAP_STACK 0x40000u
|
||||
#define MAP_SYNC 0x80000u
|
||||
#define MAP_UNINITIALIZED 0x100000u
|
||||
|
||||
extern void *mmap(
|
||||
void *addr,
|
||||
size_t length,
|
||||
int prot,
|
||||
int flags,
|
||||
int fd,
|
||||
off_t offset);
|
||||
|
||||
#endif
|
||||
18
lib/libc/io/include/sys/remote.h
Normal file
18
lib/libc/io/include/sys/remote.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef SYS_REMOTE_H_
|
||||
#define SYS_REMOTE_H_
|
||||
|
||||
#include <mango/types.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
enum sys_remote_id {
|
||||
SYS_REMOTE_NONE,
|
||||
SYS_REMOTE_NSD,
|
||||
};
|
||||
|
||||
extern bool sys_remote_get(
|
||||
enum sys_remote_id id,
|
||||
tid_t *out_tid,
|
||||
unsigned int *out_chid);
|
||||
extern void sys_remote_set(enum sys_remote_id id, tid_t tid, unsigned int chid);
|
||||
|
||||
#endif
|
||||
45
lib/libc/io/remote.c
Normal file
45
lib/libc/io/remote.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <sys/remote.h>
|
||||
|
||||
#define TID_INVALID ((tid_t) - 1)
|
||||
#define CHID_INVALID ((unsigned int)-1)
|
||||
|
||||
struct remote {
|
||||
tid_t tid;
|
||||
unsigned int chid;
|
||||
};
|
||||
|
||||
static struct remote remotes[] = {
|
||||
[SYS_REMOTE_NONE] = {.tid = TID_INVALID, .chid = CHID_INVALID},
|
||||
[SYS_REMOTE_NSD] = {.tid = TID_INVALID, .chid = CHID_INVALID},
|
||||
};
|
||||
static const size_t nr_remotes = sizeof remotes / sizeof remotes[0];
|
||||
|
||||
bool sys_remote_get(
|
||||
enum sys_remote_id id,
|
||||
tid_t *out_tid,
|
||||
unsigned int *out_chid)
|
||||
{
|
||||
if (id < 0 || id >= nr_remotes) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const struct remote *remote = &remotes[id];
|
||||
if (remote->tid == TID_INVALID || remote->chid == CHID_INVALID) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*out_tid = remote->tid;
|
||||
*out_chid = remote->chid;
|
||||
return true;
|
||||
}
|
||||
|
||||
void sys_remote_set(enum sys_remote_id id, tid_t tid, unsigned int chid)
|
||||
{
|
||||
if (id < 0 || id >= nr_remotes) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct remote *remote = &remotes[id];
|
||||
remote->tid = tid;
|
||||
remote->chid = chid;
|
||||
}
|
||||
0
lib/libc/io/unistd/close.c
Normal file
0
lib/libc/io/unistd/close.c
Normal file
6
lib/libc/io/unistd/mmap.c
Normal file
6
lib/libc/io/unistd/mmap.c
Normal file
@@ -0,0 +1,6 @@
|
||||
#include <sys/mman.h>
|
||||
|
||||
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
40
lib/libc/io/unistd/open.c
Normal file
40
lib/libc/io/unistd/open.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <errno.h>
|
||||
#include <mango/handle.h>
|
||||
#include <mango/msg.h>
|
||||
#include <rosetta/fs.h>
|
||||
#include <sys/remote.h>
|
||||
|
||||
int open(const char *path, int flags)
|
||||
{
|
||||
tid_t remote_tid;
|
||||
unsigned int remote_chid;
|
||||
if (!sys_remote_get(SYS_REMOTE_NSD, &remote_tid, &remote_chid)) {
|
||||
return __set_errno(ENXIO);
|
||||
}
|
||||
|
||||
kern_handle_t port;
|
||||
kern_status_t status = port_create(&port);
|
||||
if (status != KERN_OK) {
|
||||
return __set_errno(__errno_from_kern_status(status));
|
||||
}
|
||||
|
||||
status = port_connect(port, remote_tid, remote_chid);
|
||||
if (status != KERN_OK) {
|
||||
kern_handle_close(port);
|
||||
return __set_errno(__errno_from_kern_status(status));
|
||||
}
|
||||
|
||||
int err = SUCCESS;
|
||||
status = fs_open(port, path, flags, &err);
|
||||
if (status != KERN_OK) {
|
||||
kern_handle_close(port);
|
||||
return __set_errno(__errno_from_kern_status(status));
|
||||
}
|
||||
|
||||
if (err != SUCCESS) {
|
||||
kern_handle_close(port);
|
||||
return __set_errno(err);
|
||||
}
|
||||
|
||||
return (int)port;
|
||||
}
|
||||
21
lib/libc/io/unistd/read.c
Normal file
21
lib/libc/io/unistd/read.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <errno.h>
|
||||
#include <mango/handle.h>
|
||||
#include <mango/msg.h>
|
||||
#include <rosetta/fs.h>
|
||||
#include <sys/remote.h>
|
||||
|
||||
int read(int fd, void *buf, size_t count)
|
||||
{
|
||||
int err;
|
||||
size_t nr_read;
|
||||
kern_status_t status = fs_read(fd, count, &err, &nr_read, buf, count);
|
||||
if (status != KERN_OK) {
|
||||
return __set_errno(__errno_from_kern_status(status));
|
||||
}
|
||||
|
||||
if (err != SUCCESS) {
|
||||
return __set_errno(err);
|
||||
}
|
||||
|
||||
return nr_read;
|
||||
}
|
||||
0
lib/libc/io/unistd/write.c
Normal file
0
lib/libc/io/unistd/write.c
Normal file
Reference in New Issue
Block a user