增加fd映射层

This commit is contained in:
zhangzheng
2023-11-18 23:31:00 +08:00
parent 97b4651f31
commit 1e384bee71
18 changed files with 146 additions and 31 deletions

View File

@@ -57,7 +57,7 @@ umword_t cpio_find_file(umword_t st, umword_t en, const char *name)
{
file_info = (cpio_fs_t *)i;
if (check_magic(file_info) < 0)
if (check_magic((char *)file_info) < 0)
{
return 0;
}

View File

@@ -1,7 +1,21 @@
#pragma once
#include <u_type.h>
typedef struct fd_map_entry fd_map_entry_t;
#include <u_types.h>
int fd_map_alloc(uint16_t svr_fd, uint16_t priv_fd);
typedef struct fd_map_entry
{
uint16_t svr_fd;
uint16_t priv_fd;
uint8_t type;
uint8_t flags;
} fd_map_entry_t;
enum fd_type
{
FD_TTY,
FD_FS,
};
int fd_map_alloc(uint16_t svr_fd, uint16_t priv_fd, enum fd_type type);
int fd_map_update(int fd, fd_map_entry_t *new_entry);
int fd_map_free(int fd, fd_map_entry_t *ret_entry);
int fd_map_get(int fd, fd_map_entry_t *new_entry);

View File

@@ -38,6 +38,8 @@
long syscall_backend(long sys_inx, ...);
void fs_backend_init(void);
long be_read(long fd, char *buf, long size);
long be_write(long fd, char *buf, long size);
long be_writev(long fd, const struct iovec *iov, long iovcnt);

View File

@@ -4,12 +4,8 @@
#include <malloc.h>
#include <errno.h>
#include <pthread.h>
typedef struct fd_map_entry
{
uint16_t svr_fd;
uint16_t priv_fd;
uint8_t flags;
} fd_map_entry_t;
#include <fd_map.h>
#include <string.h>
#define FD_MAP_ROW_CN 16
#define FD_MAP_ROW_NR 16
@@ -30,7 +26,7 @@ typedef struct fd_map
static fd_map_t fd_map;
int fd_map_alloc(uint16_t svr_fd, uint16_t priv_fd)
int fd_map_alloc(uint16_t svr_fd, uint16_t priv_fd, enum fd_type type)
{
int alloc_fd = 0;
@@ -71,16 +67,31 @@ next:;
pthread_spin_unlock(&fd_map.lock);
return -EAGAIN;
}
memset(fd_map.row[row_inx], 0, sizeof(fd_map_row_t));
}
assert(fd_map.row[row_inx]->entry[inx].flags == 0);
fd_map.row[row_inx]->entry[inx].flags = 1;
fd_map.row[row_inx]->entry[inx].svr_fd = svr_fd;
fd_map.row[row_inx]->entry[inx].priv_fd = priv_fd;
fd_map.row[row_inx]->entry[inx].type = type;
fd_map.free_fd++;
pthread_spin_unlock(&fd_map.lock);
return alloc_fd;
}
int fd_map_get(int fd, fd_map_entry_t *new_entry)
{
assert(new_entry);
if (fd >= FD_MAP_TOTAL)
{
return -1;
}
int row_inx = fd / FD_MAP_ROW_CN;
int inx = fd % FD_MAP_ROW_CN;
*new_entry = fd_map.row[row_inx]->entry[inx];
return 0;
}
int fd_map_update(int fd, fd_map_entry_t *new_entry)
{
if (fd >= FD_MAP_TOTAL)
@@ -115,6 +126,10 @@ int fd_map_free(int fd, fd_map_entry_t *ret_entry)
*ret_entry = fd_map.row[row_inx]->entry[inx];
}
fd_map.row[row_inx]->entry[inx].flags = 0;
if (fd_map.free_fd > fd)
{
fd_map.free_fd = fd;
}
pthread_spin_unlock(&fd_map.lock);
}
else

View File

@@ -8,31 +8,100 @@
#include <u_log.h>
#include <u_env.h>
#include <sys/uio.h>
#include <assert.h>
#include "fd_map.h"
void fs_backend_init(void)
{
assert(fd_map_alloc(0, 0, FD_TTY) >= 0);
assert(fd_map_alloc(0, 1, FD_TTY) >= 0);
assert(fd_map_alloc(0, 2, FD_TTY) >= 0);
}
long be_read(long fd, char *buf, long size)
{
fd_map_entry_t u_fd;
int ret = fd_map_get(fd, &u_fd);
if (ret < 0)
{
return -EBADF;
}
switch (u_fd.type)
{
case FD_TTY:
{
/*TODO:*/
}
break;
case FD_FS:
{
return fs_read(fd, buf, size);
}
break;
default:
return -ENOSYS;
}
return -ENOSYS;
}
long be_write(long fd, char *buf, long size)
{
switch (fd)
fd_map_entry_t u_fd;
int ret = fd_map_get(fd, &u_fd);
if (ret < 0)
{
return -EBADF;
}
switch (u_fd.type)
{
case FD_TTY:
{
case 0:
case 1:
case 2:
ulog_write_bytes(u_get_global_env()->log_hd, buf, size);
return size;
}
break;
case FD_FS:
{
return fs_write(fd, buf, size);
}
break;
default:
return -ENOSYS;
}
return 0;
return -ENOSYS;
}
long be_writev(long fd, const struct iovec *iov, long iovcnt)
{
long wlen = 0;
for (int i = 0; i < iovcnt; i++)
{
ulog_write_bytes(u_get_global_env()->log_hd, iov[i].iov_base, iov[i].iov_len);
wlen += iov[i].iov_len;
fd_map_entry_t u_fd;
int ret = fd_map_get(fd, &u_fd);
if (ret < 0)
{
return -EBADF;
}
switch (u_fd.type)
{
case FD_TTY:
{
ulog_write_bytes(u_get_global_env()->log_hd, iov[i].iov_base, iov[i].iov_len);
wlen += iov[i].iov_len;
}
break;
case FD_FS:
{
int wsize = fs_write(fd, iov[i].iov_base, iov[i].iov_len);
wlen += wsize;
}
break;
default:
return -ENOSYS;
}
}
return wlen;
}

View File

@@ -80,7 +80,7 @@ static futex_lock_t *futex_set_addr(futex_t *ft, void *uaddr, obj_handler_t hd)
if (ft->fl_list[i].uaddr == uaddr)
{
// ft->fl_list[i].uaddr = uaddr;
if (!futex_find_thread(&ft->fl_list[i], hd))
if (!futex_find_thread(&ft->fl_list[i].fqt, hd))
{
fq_enqueue(&ft->fl_list[i].fqt, hd);
}

View File

@@ -4,6 +4,8 @@
#include "u_ipc.h"
#include "u_task.h"
#include "u_env.h"
#include "u_log.h"
#include "u_thread.h"
#include <pthread_impl.h>
long be_set_tid_address(int *val)
@@ -12,7 +14,7 @@ long be_set_tid_address(int *val)
if (pt)
{
pt->ctid = val;
pt->ctid = (unsigned long)val;
}
return THREAD_MAIN;
}

View File

@@ -5,6 +5,7 @@
#include "u_mm.h"
#include "u_app.h"
#include "u_log.h"
#include "u_arch.h"
#include <pthread_impl.h>
#include <assert.h>
static pthread_spinlock_t lock;
@@ -16,8 +17,8 @@ static void *mm_page_alloc(int page_nr)
mword_t find_inx = -1;
app_info_t *info = app_info_get(app_start_addr);
assert(info);
void *heap_addr = RAM_BASE() + info->i.heap_offset - info->i.data_offset;
size_t max_page_nr = (info->i.heap_size) / PAGE_SIZE;
void *heap_addr = (void *)((umword_t)RAM_BASE() + info->i.heap_offset - info->i.data_offset);
size_t max_page_nr = (info->i.heap_size) / MK_PAGE_SIZE;
if (max_page_nr > sizeof(mm_bitemp) * WORD_BITS)
{
ulog_write_str(LOG_PROT, "mm bitmap is to small.\n");
@@ -38,7 +39,7 @@ static void *mm_page_alloc(int page_nr)
find_inx = i * WORD_BITS + j;
}
cnt++;
if (find_inx + cnt >= max_page_nr)
if (find_inx + cnt > max_page_nr)
{
pthread_spin_unlock(&lock);
return NULL;
@@ -72,7 +73,7 @@ static void mm_page_free(int st, int nr)
{
app_info_t *info = app_info_get(app_start_addr);
assert(info);
void *heap_addr = RAM_BASE() + info->i.heap_offset - info->i.data_offset;
void *heap_addr = (void *)((umword_t)RAM_BASE() + info->i.heap_offset - info->i.data_offset);
size_t max_page_nr = (info->i.heap_size) / PAGE_SIZE;
pthread_spin_lock(&lock);
@@ -127,7 +128,7 @@ umword_t be_munmap(void *start, size_t len)
{
app_info_t *info = app_info_get(app_start_addr);
assert(info);
void *heap_addr = RAM_BASE() + info->i.heap_offset - info->i.data_offset;
void *heap_addr = (void *)((umword_t)RAM_BASE() + info->i.heap_offset - info->i.data_offset);
len = ALIGN(len, PAGE_SIZE);
// printf("munmap 0x%x, 0x%x.\n", start, len);

View File

@@ -10,8 +10,9 @@ weak void _init();
weak void _fini();
int __libc_start_main_init(int (*main)(int,char **,char **), int argc, char **argv,
void (*init_dummy)(), void(*fini_dummy)(), void(*ldso_dummy)());
void _start_c_init(void)
extern void *app_start_addr;
void _start_c_init(long *p, void *start_addr)
{
app_start_addr = start_addr; // zhangzheng add.
__libc_start_main_init(main, NULL, NULL, _init, _fini, 0);
}

View File

@@ -120,6 +120,8 @@ int __libc_start_main_init(int (*main)(int, char **, char **), int argc, char **
extern void u_env_default_init(void);
u_env_default_init();
libc.page_size = 512;
/* Barrier against hoisting application code or anything using ssp
* or thread pointer prior to its initialization above. */
lsm2_fn *stage2 = libc_start_main_stage2;

View File

@@ -65,7 +65,7 @@ struct pthread {
uintptr_t canary;
uintptr_t *dtv;
#endif
int ctid;
unsigned long ctid;
unsigned long hd;
};

View File

@@ -2,6 +2,7 @@
#include "u_types.h"
#define SYS_SCHE_HZ 1000 //!< 系统调度频率
#define MK_PAGE_SIZE 512
#define WORD_BYTES (sizeof(void *))
#define WORD_BITS (WORD_BYTES * 8)

View File

@@ -42,7 +42,7 @@ sd_t fs_open(const char *path, int flags, int mode)
}
rpc_ref_array_uint32_t_uint8_t_32_t rpc_path = {
.data = &path[ret],
.data = (uint8_t *)(&path[ret]),
.len = strlen(&path[ret]) + 1,
};
rpc_int_t rpc_flags = {

View File

@@ -109,7 +109,7 @@ RPC_GENERATION_OP2(fs_t, FS_READDIR, readdir,
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, fd,
rpc_dirent_t_t, rpc_dirent_t_t, RPC_DIR_OUT, RPC_TYPE_DATA, dir)
{
return fs_svr_readdir(fd, &dir->data);
return fs_svr_readdir(fd->data, &dir->data);
}
RPC_GENERATION_DISPATCH2(fs_t, FS_READDIR, readdir,

View File

@@ -8,6 +8,9 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} \
")
set(CMAKE_ASM_FLAGS ${CMAKE_C_FLAGS})
include_directories(
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/libc_backend/inc
)
add_subdirectory(init)
# add_subdirectory(shell)

View File

@@ -3,12 +3,15 @@
#include <board.h>
#include <u_drv.h>
#include <stdio.h>
#include <syscall_backend.h>
/* defined the LED0 pin: PC0 */
#define LED0_PIN GET_PIN(C, 0)
extern void rt_hw_board_init(void);
extern int dfs_init(void);
int main(void)
{
fs_backend_init();
printf("test\n");
u_drv_init();
/* init board */

View File

@@ -1,5 +1,5 @@
#define HEAP_SIZE 0
#define HEAP_SIZE 512
#define STACK_SIZE 1024 * 2
#if defined(__CC_ARM)

View File

@@ -15,11 +15,13 @@
#include "u_rpc_svr.h"
#include "namespace.h"
#include "ns_svr.h"
#include "syscall_backend.h"
#include <assert.h>
#include <stdio.h>
extern void futex_init(void);
int main(int argc, char *args[])
{
fs_backend_init();
// futex_init();
ulog_write_str(LOG_PROT, "init..\n");
#if 0