fs: add ramfs
This commit is contained in:
parent
a7c526d2a6
commit
750ffa2e16
13 changed files with 355 additions and 55 deletions
|
|
@ -148,4 +148,7 @@ void pci_init()
|
||||||
host->base = hostbase;
|
host->base = hostbase;
|
||||||
host_probe_devices(host);
|
host_probe_devices(host);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
krdy("PCI ready\n");
|
||||||
}
|
}
|
||||||
|
DRIVER_ENTRY_PRIORITY(pci_init, 1);
|
||||||
|
|
|
||||||
|
|
@ -9,35 +9,35 @@
|
||||||
static mem_cache_t *inode_slab = NULL;
|
static mem_cache_t *inode_slab = NULL;
|
||||||
static mem_cache_t *dirent_slab = NULL;
|
static mem_cache_t *dirent_slab = NULL;
|
||||||
|
|
||||||
dirent_t *inode_find_dirent(inode_t *inode, char *name)
|
dirent_t *inode_find_dirent(inode_t *inode, const char *name)
|
||||||
{
|
{
|
||||||
list_item_t *p = inode->dentries.head;
|
list_item_t *p = inode->dentries.head;
|
||||||
|
|
||||||
while (p) {
|
while (p) {
|
||||||
dirent_t *dirent = (void *)p->ptr;
|
dirent_t *dirent = (void *)p->ptr;
|
||||||
if (!strncmp(name, dirent->name, dirent->name_len)) {
|
if (!strncmp(name, dirent->name, dirent->name_len))
|
||||||
return dirent;
|
return dirent;
|
||||||
}
|
|
||||||
p = p->next;
|
p = p->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
dirent_t *dirent_create(const char *name, inode_t *inode, u64 flags)
|
dirent_t *dirent_create(const char *name)
|
||||||
{
|
{
|
||||||
dirent_t *ret = slab_alloc(dirent_slab);
|
dirent_t *ret = slab_alloc(dirent_slab);
|
||||||
|
|
||||||
if (ret) {
|
if (!ret)
|
||||||
ret->inode = inode;
|
return NULL;
|
||||||
|
|
||||||
ret->flags = flags;
|
ret->rec_len = sizeof(dirent_t);
|
||||||
ret->rec_len = sizeof(dirent_t);
|
|
||||||
|
|
||||||
ret->name_len = strlen(name);
|
ret->name_len = strlen(name);
|
||||||
ret->name = kalloc(ret->name_len + 1, ALLOC_KERN);
|
ret->name = strdup(name);
|
||||||
ret->name[ret->name_len] = 0;
|
|
||||||
memcpy(ret->name, name, ret->name_len);
|
if (!ret->name) {
|
||||||
|
slab_free(dirent_slab, ret);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
@ -69,7 +69,9 @@ void inode_free(inode_t *inode)
|
||||||
void inode_init()
|
void inode_init()
|
||||||
{
|
{
|
||||||
inode_slab = slab_create("inode_slab", sizeof(inode_t), 64 * KB, 0x8);
|
inode_slab = slab_create("inode_slab", sizeof(inode_t), 64 * KB, 0x8);
|
||||||
if(!inode_slab)panic("failed to create inode_slab\n");
|
if (!inode_slab)
|
||||||
|
panic("failed to create inode_slab\n");
|
||||||
dirent_slab = slab_create("dentry_slab", sizeof(dirent_t), 64 * KB, 0x8);
|
dirent_slab = slab_create("dentry_slab", sizeof(dirent_t), 64 * KB, 0x8);
|
||||||
if(!dirent_slab)panic("failed to create dirent_slab\n");
|
if (!dirent_slab)
|
||||||
|
panic("failed to create dirent_slab\n");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
161
fs/vfs/ramfs.c
Normal file
161
fs/vfs/ramfs.c
Normal file
|
|
@ -0,0 +1,161 @@
|
||||||
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
/* ramfs: in-ram filesystem
|
||||||
|
*
|
||||||
|
* notes:
|
||||||
|
* inode->ino_num is a the length of the current kalloc buffer
|
||||||
|
* inode->data is a pointer to the direct data block of the file
|
||||||
|
* dirent->ino_num is a pointer to the inode
|
||||||
|
*/
|
||||||
|
#include "fs/vfs.h"
|
||||||
|
#include "lib/errno.h"
|
||||||
|
#include "lib/list.h"
|
||||||
|
#include <kernel/common.h>
|
||||||
|
#include <kernel/slab.h>
|
||||||
|
#include <lib/string.h>
|
||||||
|
#include <fs/ramfs.h>
|
||||||
|
|
||||||
|
static int alloc_inode(fs_t *fs, inode_t **out)
|
||||||
|
{
|
||||||
|
const size_t alloc_len = 0x4000;
|
||||||
|
inode_t *ret = inode_create();
|
||||||
|
if (!ret)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
memset(ret, 0, sizeof(*ret));
|
||||||
|
ret->fs = fs;
|
||||||
|
ret->ino_num = alloc_len;
|
||||||
|
ret->data = kalloc(alloc_len, ALLOC_KERN);
|
||||||
|
*out = ret;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int free_inode(fs_t *fs, inode_t *inode)
|
||||||
|
{
|
||||||
|
inode_free(inode);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int open_inode(fs_t *fs, u64 id, inode_t **out)
|
||||||
|
{
|
||||||
|
if (!id)
|
||||||
|
return -ENOENT;
|
||||||
|
|
||||||
|
*out = (void *)id;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t read(inode_t *inode, char *buf, size_t size, size_t offset)
|
||||||
|
{
|
||||||
|
if (offset < inode->file_size) {
|
||||||
|
size = MIN(size, inode->file_size - offset);
|
||||||
|
memcpy(buf, inode->data + offset, size);
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t write(inode_t *inode, char *buf, size_t size, size_t offset)
|
||||||
|
{
|
||||||
|
size_t cur_len = inode->ino_num;
|
||||||
|
if (offset + size >= cur_len) {
|
||||||
|
size_t new_size = npow2(size + offset);
|
||||||
|
void *new_buf = krealloc(inode->data, new_size, ALLOC_KERN);
|
||||||
|
|
||||||
|
if (!new_buf)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
inode->ino_num = new_size;
|
||||||
|
inode->data = new_buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(inode->data + offset, buf, size);
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int creat(inode_t *inode, const char *name, u64 mode, dirent_t **out)
|
||||||
|
{
|
||||||
|
if (inode_find_dirent(inode, name))
|
||||||
|
return -EEXIST;
|
||||||
|
|
||||||
|
dirent_t *dirent = dirent_create(name);
|
||||||
|
if (!dirent)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
inode_t *new_ino;
|
||||||
|
int ret = alloc_inode(inode->fs, &new_ino);
|
||||||
|
|
||||||
|
if (ret < 0)
|
||||||
|
goto out_1;
|
||||||
|
|
||||||
|
dirent->inode = new_ino;
|
||||||
|
inode->mode = mode;
|
||||||
|
|
||||||
|
list_insert(&inode->dentries, dirent);
|
||||||
|
*out = dirent;
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
out_1:
|
||||||
|
dirent_free(dirent);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int mkdir(inode_t *inode, const char *name, dirent_t **out)
|
||||||
|
{
|
||||||
|
dirent_t *d;
|
||||||
|
int ret = creat(inode, name, INODE_DIRECTORY, &d);
|
||||||
|
if (ret >= 0)
|
||||||
|
*out = d;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int link(inode_t *inode, inode_t *link_inode, const char *name, dirent_t **out)
|
||||||
|
{
|
||||||
|
dirent_t *d;
|
||||||
|
int ret = creat(inode, name, INODE_SYMLINK, &d);
|
||||||
|
if (ret >= 0)
|
||||||
|
*out = d;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int unlink(inode_t *inode, const char *name)
|
||||||
|
{
|
||||||
|
if (inode->mode != INODE_DIRECTORY)
|
||||||
|
return -ENOTDIR;
|
||||||
|
|
||||||
|
list_item_t *item = inode->dentries.head;
|
||||||
|
size_t name_len = strlen(name);
|
||||||
|
while (item) {
|
||||||
|
dirent_t *ent = item->ptr;
|
||||||
|
if (!strncmp(name, ent->name, name_len)) {
|
||||||
|
list_remove(item);
|
||||||
|
dirent_free(ent);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
item = item->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -ENOENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int readdir(inode_t *parent)
|
||||||
|
{
|
||||||
|
if (parent->mode != INODE_DIRECTORY)
|
||||||
|
return -ENOTDIR;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static dirent_t *ramfs_mount(int flags, struct block_device *bdev)
|
||||||
|
{
|
||||||
|
static struct inode_ops ramfs_inode_ops = {
|
||||||
|
.read = read, .write = write, .mkdir = mkdir, .creat = creat, .link = link, .unlink = unlink, .readdir = readdir};
|
||||||
|
static struct fs_ops ramfs_ops = {.alloc_inode = alloc_inode, .free_inode = free_inode, .open_inode = open_inode};
|
||||||
|
}
|
||||||
|
|
||||||
|
void ramfs_init()
|
||||||
|
{
|
||||||
|
struct fs_type ramfs_type = {.name = "ramfs", .fs_mount = ramfs_mount};
|
||||||
|
}
|
||||||
|
DRIVER_ENTRY_PRIORITY(ramfs_init, 2);
|
||||||
87
fs/vfs/vfs.c
87
fs/vfs/vfs.c
|
|
@ -1,44 +1,87 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
#include "lib/rbtree.h"
|
||||||
|
#include <kernel/common.h>
|
||||||
#include <kernel/slab.h>
|
#include <kernel/slab.h>
|
||||||
|
|
||||||
#include <lib/list.h>
|
#include <lib/list.h>
|
||||||
#include <lib/string.h>
|
#include <lib/string.h>
|
||||||
#include <fs/vfs.h>
|
#include <fs/vfs.h>
|
||||||
|
|
||||||
static fs_t root_fs = {0};
|
#include <lib/errno.h>
|
||||||
|
|
||||||
void vfs_mount(inode_t *mount_point, fs_t *fs_to_mount)
|
static fs_t *root_fs = NULL;
|
||||||
|
|
||||||
|
static list_t fs_type_list = {0};
|
||||||
|
|
||||||
|
int vfs_mount(inode_t *mount_point, fs_t *fs_to_mount)
|
||||||
{
|
{
|
||||||
if (!mount_point) {
|
if (!mount_point) {
|
||||||
|
if (root_fs) {
|
||||||
}
|
return -EBUSY;
|
||||||
|
} else {
|
||||||
|
root_fs = fs_to_mount;
|
||||||
}
|
|
||||||
|
|
||||||
void vfs_close_inode(inode_t *inode)
|
|
||||||
{
|
|
||||||
list_item_t *entry = inode->dentries.head;
|
|
||||||
while (entry) {
|
|
||||||
dirent_t *dirent = entry->ptr;
|
|
||||||
|
|
||||||
if (dirent->inode) {
|
|
||||||
vfs_close_inode(dirent->inode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inode_free(inode);
|
|
||||||
|
|
||||||
entry = entry->next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void vfs_open_inode(const char *path)
|
static int iter_inode_dirent(inode_t *ino, const char *name, inode_t **out)
|
||||||
{
|
{
|
||||||
|
fs_t *fs = ino->fs;
|
||||||
|
dirent_t *d = inode_find_dirent(ino, name);
|
||||||
|
if (d) {
|
||||||
|
if (d->inode)
|
||||||
|
*out = d->inode;
|
||||||
|
else
|
||||||
|
return fs->fs_ops.open_inode(fs, d->ino_num, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int vfs_get_inode(const char *path, inode_t *ret)
|
||||||
|
{
|
||||||
|
inode_t *ino = root_fs->root_inode;
|
||||||
|
|
||||||
|
char name[512] = {0};
|
||||||
|
|
||||||
|
char c;
|
||||||
|
int i = 0;
|
||||||
|
while (i < 256 && ino) {
|
||||||
|
c = *path++;
|
||||||
|
if (c == '/' || c == '\0') {
|
||||||
|
name[i] = 0;
|
||||||
|
i = 0;
|
||||||
|
|
||||||
|
int res = iter_inode_dirent(ino, name, &ino);
|
||||||
|
if (res)
|
||||||
|
return res;
|
||||||
|
|
||||||
|
if (c == '\0')
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
name[i] = c;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -ENOENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
void vfs_register(struct fs_type *fs)
|
||||||
|
{
|
||||||
|
struct fs_type *ins = kalloc(sizeof(*fs), ALLOC_KERN);
|
||||||
|
memcpy(ins, fs, sizeof(*fs));
|
||||||
|
list_insert(&fs_type_list, ins);
|
||||||
}
|
}
|
||||||
|
|
||||||
void vfs_init()
|
void vfs_init()
|
||||||
{
|
{
|
||||||
void inode_init();
|
void inode_init();
|
||||||
inode_init();
|
inode_init();
|
||||||
memset(&root_fs, 0, sizeof(root_fs));
|
root_fs = kalloc(sizeof(fs_t), ALLOC_KERN);
|
||||||
|
memset(root_fs, 0, sizeof(*root_fs));
|
||||||
|
|
||||||
|
list_create(&fs_type_list);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
8
include/fs/ramfs.h
Normal file
8
include/fs/ramfs.h
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
#ifndef _RAMFS_H_
|
||||||
|
#define _RAMFS_H_
|
||||||
|
|
||||||
|
#include <fs/vfs.h>
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* _RAMFS_H_ */
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
#define _VFS_H_
|
#define _VFS_H_
|
||||||
|
|
||||||
#include <kernel/spinlock.h>
|
#include <kernel/spinlock.h>
|
||||||
|
#include <drivers/block.h>
|
||||||
#include <lib/rwlock.h>
|
#include <lib/rwlock.h>
|
||||||
#include <lib/list.h>
|
#include <lib/list.h>
|
||||||
|
|
||||||
|
|
@ -19,19 +20,30 @@
|
||||||
|
|
||||||
#define DIRENT_TYPE(dirent) ((dirent)->flags & 0x7)
|
#define DIRENT_TYPE(dirent) ((dirent)->flags & 0x7)
|
||||||
|
|
||||||
|
#define INODE_UNKNOWN 0x0
|
||||||
|
#define INODE_REGULAR 0x1
|
||||||
|
#define INODE_DIRECTORY 0x2
|
||||||
|
#define INODE_CHRDEV 0x3
|
||||||
|
#define INODE_BLKDEV 0x4
|
||||||
|
#define INODE_FIFO 0x5
|
||||||
|
#define INODE_SOCKET 0x6
|
||||||
|
#define INODE_SYMLINK 0x7
|
||||||
|
|
||||||
typedef struct _inode inode_t;
|
typedef struct _inode inode_t;
|
||||||
typedef struct _dirent dirent_t;
|
typedef struct _dirent dirent_t;
|
||||||
typedef struct _fs fs_t;
|
typedef struct _fs fs_t;
|
||||||
|
|
||||||
struct fs_ops {
|
struct fs_type {
|
||||||
int (*alloc_inode)(fs_t *fs, inode_t *out);
|
char name[256];
|
||||||
int (*free_inode)(fs_t *fs, inode_t *inode);
|
dirent_t *(*fs_mount)(int flags, struct block_device *bdev);
|
||||||
|
void (*fs_umount)(fs_t *fs);
|
||||||
|
bool (*fs_detect)(struct block_device *bdev);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct fs_superblock {
|
struct fs_ops {
|
||||||
u64 type;
|
int (*alloc_inode)(fs_t *fs, inode_t **out);
|
||||||
size_t block_size;
|
int (*free_inode)(fs_t *fs, inode_t *inode);
|
||||||
size_t n_blocks;
|
int (*open_inode)(fs_t *fs, u64 id, inode_t **out);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct inode_ops {
|
struct inode_ops {
|
||||||
|
|
@ -39,36 +51,50 @@ struct inode_ops {
|
||||||
ssize_t (*read)(inode_t *inode, char *buf, size_t size, size_t offset);
|
ssize_t (*read)(inode_t *inode, char *buf, size_t size, size_t offset);
|
||||||
ssize_t (*write)(inode_t *inode, char *buf, size_t size, size_t offset);
|
ssize_t (*write)(inode_t *inode, char *buf, size_t size, size_t offset);
|
||||||
/* for directory files */
|
/* for directory files */
|
||||||
int (*mkdir)(inode_t *inode, const char *name, dirent_t *out);
|
int (*mkdir)(inode_t *inode, const char *name, dirent_t **out);
|
||||||
int (*creat)(inode_t *inode, const char *name, dirent_t *out);
|
int (*creat)(inode_t *inode, const char *name, dirent_t **out);
|
||||||
int (*link)(inode_t *inode, const char *name, dirent_t *out);
|
int (*link)(inode_t *inode, const char *name, dirent_t **out);
|
||||||
int (*unlink)(inode_t *inode, const char *name);
|
int (*unlink)(inode_t *inode, const char *name);
|
||||||
int (*lookup)(inode_t *parent, const char *name, dirent_t *out);
|
int (*readdir)(inode_t *parent);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _fs {
|
struct _fs {
|
||||||
struct fs_superblock superblock;
|
u64 type;
|
||||||
struct _inode *root_inode;
|
size_t block_size;
|
||||||
u64 fs_type;
|
size_t n_blocks;
|
||||||
|
|
||||||
|
inode_t *root_inode;
|
||||||
|
|
||||||
spinlock_t lock;
|
spinlock_t lock;
|
||||||
struct fs_ops fs_ops;
|
struct fs_ops fs_ops;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _dirent {
|
struct _dirent {
|
||||||
inode_t *inode;
|
inode_t *inode;
|
||||||
|
u64 ino_num;
|
||||||
|
|
||||||
size_t rec_len;
|
size_t rec_len;
|
||||||
size_t name_len;
|
size_t name_len;
|
||||||
size_t flags;
|
|
||||||
|
u64 flags;
|
||||||
|
u64 type;
|
||||||
|
|
||||||
char *name;
|
char *name;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _inode {
|
struct _inode {
|
||||||
|
fs_t *fs;
|
||||||
|
u64 mode;
|
||||||
u64 ino_num;
|
u64 ino_num;
|
||||||
|
u64 file_size;
|
||||||
|
|
||||||
list_t dentries;
|
list_t dentries;
|
||||||
|
|
||||||
void *data;
|
void *data;
|
||||||
|
|
||||||
u64 refcount;
|
u64 refcount;
|
||||||
rwlock_t rwlock;
|
rwlock_t rwlock;
|
||||||
struct fs *fs;
|
|
||||||
struct _inode *parent_inode;
|
struct _inode *parent_inode;
|
||||||
struct inode_ops i_ops;
|
struct inode_ops i_ops;
|
||||||
};
|
};
|
||||||
|
|
@ -76,5 +102,8 @@ struct _inode {
|
||||||
void vfs_init();
|
void vfs_init();
|
||||||
inode_t *inode_create();
|
inode_t *inode_create();
|
||||||
void inode_free(inode_t *inode);
|
void inode_free(inode_t *inode);
|
||||||
|
dirent_t *inode_find_dirent(inode_t *inode, const char *name);
|
||||||
|
dirent_t *dirent_create(const char *name);
|
||||||
|
void dirent_free(dirent_t *dirent);
|
||||||
|
|
||||||
#endif /* _VFS_H_ */
|
#endif /* _VFS_H_ */
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@
|
||||||
#include <kernel/types.h>
|
#include <kernel/types.h>
|
||||||
|
|
||||||
#include <lib/stdio.h>
|
#include <lib/stdio.h>
|
||||||
|
#include <lib/math.h>
|
||||||
|
#include <lib/errno.h>
|
||||||
|
|
||||||
#define PACKED __attribute__((packed))
|
#define PACKED __attribute__((packed))
|
||||||
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
||||||
|
|
@ -26,6 +28,9 @@
|
||||||
_panic(); \
|
_panic(); \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define DRIVER_ENTRY(fn) static void (*__initcall_##fn)(void) __attribute((used, section(".initcall"))) = fn
|
||||||
|
#define DRIVER_ENTRY_PRIORITY(fn, pr) static void (*__initcall_##fn)(void) __attribute((used, section(".initcall." #pr))) = fn
|
||||||
|
|
||||||
void yield();
|
void yield();
|
||||||
void _panic();
|
void _panic();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ void slab_free(mem_cache_t *cache, void *ptr);
|
||||||
|
|
||||||
void *kalloc(size_t size, u64 flags);
|
void *kalloc(size_t size, u64 flags);
|
||||||
void *kzalloc(size_t size, u64 flags);
|
void *kzalloc(size_t size, u64 flags);
|
||||||
|
void *krealloc(void *old, size_t size, u64 flags);
|
||||||
void kfree(void *ptr);
|
void kfree(void *ptr);
|
||||||
|
|
||||||
void kalloc_init();
|
void kalloc_init();
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
|
||||||
void *memcpy(void *dest, const void *src, size_t num);
|
void *memcpy(void *dest, const void *src, size_t num);
|
||||||
int memcmp(const void *aa, const void *bb, size_t num);
|
int memcmp(const void *aa, const void *bb, size_t num);
|
||||||
int strcmp(const char *a, const char *b);
|
int strcmp(const char *a, const char *b);
|
||||||
|
|
@ -17,6 +16,7 @@ void *memset(void *str, int c, size_t n);
|
||||||
char *strcpy(char *dest, const char *src);
|
char *strcpy(char *dest, const char *src);
|
||||||
char *strncpy(char *dest, const char *src, size_t n);
|
char *strncpy(char *dest, const char *src, size_t n);
|
||||||
char *strtok(char *str, const char *delim, char **llast);
|
char *strtok(char *str, const char *delim, char **llast);
|
||||||
|
char *strdup(const char *s);
|
||||||
bool strempty(const char *s);
|
bool strempty(const char *s);
|
||||||
char **strsplit(const char *str, char delim, int *num);
|
char **strsplit(const char *str, char delim, int *num);
|
||||||
size_t strlen(const char *c);
|
size_t strlen(const char *c);
|
||||||
|
|
|
||||||
13
init/main.c
13
init/main.c
|
|
@ -24,8 +24,17 @@
|
||||||
|
|
||||||
#define P_STACK_SIZE 0x8000
|
#define P_STACK_SIZE 0x8000
|
||||||
|
|
||||||
|
typedef void (*initcall_t)(void);
|
||||||
|
|
||||||
|
extern initcall_t __initcall_start, __initcall_end;
|
||||||
struct tty *tty_printf = NULL;
|
struct tty *tty_printf = NULL;
|
||||||
|
|
||||||
|
static void do_initcalls()
|
||||||
|
{
|
||||||
|
for (initcall_t *fn = &__initcall_start; fn < &__initcall_end; fn++)
|
||||||
|
(*fn)();
|
||||||
|
}
|
||||||
|
|
||||||
static void kmain()
|
static void kmain()
|
||||||
{
|
{
|
||||||
/* data types */
|
/* data types */
|
||||||
|
|
@ -50,7 +59,9 @@ static void kmain()
|
||||||
arch_init();
|
arch_init();
|
||||||
|
|
||||||
page_init();
|
page_init();
|
||||||
pci_init();
|
|
||||||
|
do_initcalls();
|
||||||
|
|
||||||
krdy("Welcome to vbos!\n" COL_RESET);
|
krdy("Welcome to vbos!\n" COL_RESET);
|
||||||
|
|
||||||
yield();
|
yield();
|
||||||
|
|
|
||||||
26
lib/string.c
26
lib/string.c
|
|
@ -1,5 +1,6 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
#include <kernel/types.h>
|
#include <kernel/types.h>
|
||||||
|
#include <kernel/slab.h>
|
||||||
#include <lib/string.h>
|
#include <lib/string.h>
|
||||||
|
|
||||||
void *memcpy(void *dest, const void *src, size_t num)
|
void *memcpy(void *dest, const void *src, size_t num)
|
||||||
|
|
@ -28,7 +29,7 @@ int strncmp(const char *a, const char *b, size_t n)
|
||||||
for (size_t i = 0; i < n; i++) {
|
for (size_t i = 0; i < n; i++) {
|
||||||
if (a[i] != b[i])
|
if (a[i] != b[i])
|
||||||
return a[i] - b[i];
|
return a[i] - b[i];
|
||||||
if (!a[i])
|
if (!a[i] || !b[i])
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -136,26 +137,37 @@ char *strtok(char *str, const char *delim, char **llast)
|
||||||
(*llast)++;
|
(*llast)++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!(*ret))
|
if (!(*ret))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *strdup(const char *s)
|
||||||
|
{
|
||||||
|
size_t len = strlen(s);
|
||||||
|
char *ret = kalloc(len + 1, ALLOC_KERN);
|
||||||
|
if (!ret)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
ret[len] = 0;
|
||||||
|
strcpy(ret, s);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
bool strempty(const char *s)
|
bool strempty(const char *s)
|
||||||
{
|
{
|
||||||
if(!s)
|
if (!s)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if(!*s)
|
if (!*s)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
size_t i = strlen(s);
|
size_t i = strlen(s);
|
||||||
while(i--) {
|
while (i--) {
|
||||||
if(s[i] != ' ')
|
if (s[i] != ' ')
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,13 +15,19 @@ SECTIONS
|
||||||
{
|
{
|
||||||
. = 0xffffffff80000000;
|
. = 0xffffffff80000000;
|
||||||
__kernel_start = .;
|
__kernel_start = .;
|
||||||
|
|
||||||
.text : {
|
.text : {
|
||||||
*(.text .text.*)
|
*(.text .text.*)
|
||||||
} :text
|
} :text
|
||||||
|
|
||||||
. += CONSTANT(MAXPAGESIZE);
|
. += CONSTANT(MAXPAGESIZE);
|
||||||
|
|
||||||
|
.initcall ALIGN(8) : {
|
||||||
|
__initcall_start = .;
|
||||||
|
*(SORT(.initcall.*))
|
||||||
|
*(.initcall)
|
||||||
|
__initcall_end = .;
|
||||||
|
} :rodata
|
||||||
|
|
||||||
.rodata ALIGN (0x1000) : {
|
.rodata ALIGN (0x1000) : {
|
||||||
*(.rodata .rodata.*)
|
*(.rodata .rodata.*)
|
||||||
} :rodata
|
} :rodata
|
||||||
|
|
|
||||||
19
mem/alloc.c
19
mem/alloc.c
|
|
@ -114,6 +114,25 @@ out_1:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *krealloc(void *old, size_t size, u64 flags)
|
||||||
|
{
|
||||||
|
rbnode_t *node = rbt_search(&kalloc_tree, (u64)old);
|
||||||
|
|
||||||
|
if (!node)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
struct alloc_entry *entry = (void *)node->value;
|
||||||
|
|
||||||
|
void *new = kalloc(size, flags);
|
||||||
|
if (!new)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
memcpy(new, old, entry->len);
|
||||||
|
kfree(old);
|
||||||
|
|
||||||
|
return new;
|
||||||
|
}
|
||||||
|
|
||||||
void kalloc_init()
|
void kalloc_init()
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < n_slab_sizes; i++) {
|
for (size_t i = 0; i < n_slab_sizes; i++) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue