Skip to content

Commit

Permalink
kernel: Add preliminary C support for many headers
Browse files Browse the repository at this point in the history
This commit fixes up a variety of headers for C support on things such
as filesystem drivers. It has a couple of hacks, and some stuff just
isn't there yet (struct process, for one).

Signed-off-by: Pedro Falcato <pedro.falcato@gmail.com>
heatd committed Jan 19, 2024
1 parent d66e97e commit cea0cc9
Showing 29 changed files with 355 additions and 269 deletions.
52 changes: 4 additions & 48 deletions kernel/include/onyx/atomic.hpp
Original file line number Diff line number Diff line change
@@ -9,10 +9,6 @@

#include <stddef.h>

#include <onyx/conditional.h>
#include <onyx/enable_if.h>
#include <onyx/is_integral.h>

#ifdef __GNUG__
enum class mem_order : int
{
@@ -38,16 +34,17 @@ enum class mem_order : int
#endif

template <typename type>
class atomic_primitive
class atomic
{
protected:
type val;

public:
constexpr atomic_primitive(type v) : val{v}
constexpr atomic(type v) : val{v}
{
}
constexpr atomic_primitive() : val{}

constexpr atomic() : val{}
{
}

@@ -134,47 +131,6 @@ class atomic_primitive
return __atomic_compare_exchange_n(&val, &expected, desired, false, (int) order,
(int) order);
}
};

template <typename type>
class atomic_complex_type
{
protected:
type val;

public:
virtual type load(mem_order order = mem_order::seq_cst) = 0;

virtual void store(type t, mem_order order = mem_order::seq_cst) = 0;

virtual type add_fetch(type v, mem_order order = mem_order::seq_cst) = 0;
virtual type fetch_add(type v, mem_order order = mem_order::seq_cst) = 0;
virtual type sub_fetch(type v, mem_order order = mem_order::seq_cst) = 0;
virtual type fetch_sub(type v, mem_order order = mem_order::seq_cst) = 0;
virtual type and_fetch(type v, mem_order order = mem_order::seq_cst) = 0;
virtual type fetch_and(type v, mem_order order = mem_order::seq_cst) = 0;
virtual type or_fetch(type v, mem_order order = mem_order::seq_cst) = 0;
virtual type fetch_or(type v, mem_order order = mem_order::seq_cst) = 0;
virtual type xor_fetch(type v, mem_order order = mem_order::seq_cst) = 0;
virtual type fetch_xor(type v, mem_order order = mem_order::seq_cst) = 0;
};

template <typename type>
class atomic : public conditional<is_integral_v<type>, atomic_primitive<type>,
atomic_complex_type<type>>::type
{
private:
public:
constexpr atomic(type v)
: conditional<is_integral_v<type>, atomic_primitive<type>, atomic_complex_type<type>>::type(
v)
{
}
constexpr atomic()
: conditional<is_integral_v<type>, atomic_primitive<type>,
atomic_complex_type<type>>::type()
{
}

/* pre increment */
type operator++()
4 changes: 3 additions & 1 deletion kernel/include/onyx/bio.h
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ void bio_free(struct bio_req *req);

static inline void bio_init(struct bio_req *req)
{
*req = {};
*req = (struct bio_req){};
req->b_ref = 1;
}

@@ -57,6 +57,8 @@ static inline void bio_put(struct bio_req *req)
*/
int bio_submit_req_wait(struct blockdev *dev, struct bio_req *req);

int bio_submit_request(struct blockdev *dev, struct bio_req *req);

/**
* @brief Complete a bio req
* Callable from softirq context, and lower
2 changes: 0 additions & 2 deletions kernel/include/onyx/block.h
Original file line number Diff line number Diff line change
@@ -104,8 +104,6 @@ int blkdev_flush(struct blockdev *dev);
*/
int blkdev_power(int op, struct blockdev *dev);

int bio_submit_request(struct blockdev *dev, struct bio_req *req);

static inline bool block_get_device_letter_from_id(unsigned int id, cul::slice<char> buffer)
{
if (id > 26)
8 changes: 6 additions & 2 deletions kernel/include/onyx/buffer.h
Original file line number Diff line number Diff line change
@@ -13,6 +13,8 @@
#include <onyx/mm/flush.h>
#include <onyx/page.h>

__BEGIN_CDECLS

/* block_buf represents a filesystem block(works kind of like a buffer_head in linux).
* It keeps information like whether the block is dirty, the page it's stored on, the offset, etc.
* It's supposed to be used by filesystems only, for metadata.
@@ -136,10 +138,12 @@ void block_buf_dirty_inode(struct block_buf *buf, struct inode *inode);
*/
void block_buf_tear_down_assoc(struct vm_object *object);

#ifdef __cplusplus

void page_remove_block_buf(struct page *page, size_t offset, size_t end);

__END_CDECLS

#ifdef __cplusplus

class auto_block_buf
{
private:
6 changes: 6 additions & 0 deletions kernel/include/onyx/compiler.h
Original file line number Diff line number Diff line change
@@ -166,4 +166,10 @@ inline void write_once(const Type& t, Type val)
#define __nocov
#endif

#ifdef __cplusplus
#define __deprecated [[deprecated]]
#else
#define __deprecated
#endif

#endif /* COMPILER_H */
4 changes: 2 additions & 2 deletions kernel/include/onyx/cpu.h
Original file line number Diff line number Diff line change
@@ -241,7 +241,7 @@
#define X86_CPU_MANUFACTURER_AMD 1
#define X86_CPU_MANUFACTURER_UNKNOWN 3

using cpu_t = struct cpu
typedef struct cpu
{
char manuid[13];
char brandstr[48];
@@ -254,7 +254,7 @@ using cpu_t = struct cpu
unsigned long manufacturer;
/* Add more as needed */
uint64_t caps[8];
};
} cpu_t;

__attribute__((hot)) bool x86_has_cap(int cap);
bool x86_has_usable_tsc(void);
11 changes: 8 additions & 3 deletions kernel/include/onyx/file.h
Original file line number Diff line number Diff line change
@@ -7,13 +7,16 @@
#define _ONYX_FILE_H

#include <errno.h>
#include <uapi/fcntl.h>

#include <onyx/panic.h>
#include <onyx/vfs.h>

#include <uapi/fcntl.h>

struct ioctx;

__BEGIN_CDECLS

void file_do_cloexec(struct ioctx *ctx);
int open_with_vnode(struct file *node, int flags);
struct file *get_file_description(int fd);
@@ -31,7 +34,7 @@ void file_cache_init();
*
* @return Pointer to struct file, or nullptr
*/
file *file_alloc();
struct file *file_alloc();

/**
* @brief Free a struct file
@@ -43,7 +46,7 @@ void file_free(struct file *file);
int allocate_file_descriptor_table(struct process *process);
int copy_file_descriptors(struct process *process, struct ioctx *ctx);
struct file *get_dirfd_file(int dirfd);
void process_destroy_file_descriptors(process *process);
void process_destroy_file_descriptors(struct process *process);

#define OPEN_FLAGS_ACCESS_MODE(flags) (flags & 0x3)

@@ -64,6 +67,8 @@ static inline unsigned int open_to_file_access_flags(int open_flgs)

bool fd_may_access(struct file *f, unsigned int access);

__END_CDECLS

#ifdef __cplusplus

class auto_file
7 changes: 5 additions & 2 deletions kernel/include/onyx/filemap.h
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@

struct file;
struct page;
struct iovec_iter;

/**
* @brief Read from a generic file (using the page cache) using iovec_iter
@@ -26,7 +27,8 @@ struct page;
* @param flags Flags
* @return Read bytes, or negative error code
*/
ssize_t filemap_read_iter(struct file *filp, size_t off, iovec_iter *iter, unsigned int flags);
ssize_t filemap_read_iter(struct file *filp, size_t off, struct iovec_iter *iter,
unsigned int flags);

/**
* @brief Write to a generic file (using the page cache) using iovec_iter
@@ -37,7 +39,8 @@ ssize_t filemap_read_iter(struct file *filp, size_t off, iovec_iter *iter, unsig
* @param flags Flags
* @return Written bytes, or negative error code
*/
ssize_t filemap_write_iter(struct file *filp, size_t off, iovec_iter *iter, unsigned int flags);
ssize_t filemap_write_iter(struct file *filp, size_t off, struct iovec_iter *iter,
unsigned int flags);

#define FILEMAP_MARK_DIRTY RA_MARK_0

19 changes: 17 additions & 2 deletions kernel/include/onyx/mm/slab.h
Original file line number Diff line number Diff line change
@@ -14,17 +14,28 @@
#include <onyx/mm/kasan.h>
#include <onyx/spinlock.h>

#ifndef CONFIG_SMP_NR_CPUS
#define CONFIG_SMP_NR_CPUS 64
#endif

#ifdef __cplusplus
#include <onyx/atomic.hpp>
#define ATOMIC_TYPE(type) atomic<type>
#else
#define ATOMIC_TYPE(type) type
#endif

#define SLAB_CACHE_PERCPU_MAGAZINE_SIZE 128

struct slab_cache_percpu_context
{
void *magazine[SLAB_CACHE_PERCPU_MAGAZINE_SIZE];
int size;
atomic<int> touched;
ATOMIC_TYPE(int) touched;
} __align_cache;

#undef ATOMIC_TYPE

struct slab_cache
{
const char *name;
@@ -42,14 +53,16 @@ struct slab_cache
size_t nfullslabs;
struct list_head cache_list_node;
unsigned int flags;
spinlock lock;
struct spinlock lock;
void (*ctor)(void *);
int mag_limit;
// TODO: This is horrible. We need a way to allocate percpu memory,
// and then either trim it or grow it when CPUs come online.
struct slab_cache_percpu_context pcpu[CONFIG_SMP_NR_CPUS] __align_cache;
};

__BEGIN_CDECLS

#define KMEM_CACHE_HWALIGN (1 << 0)
#define KMEM_CACHE_VMALLOC (1 << 1)
#define KMEM_CACHE_NOPCPU (1 << 2)
@@ -143,4 +156,6 @@ void kmem_cache_print_slab_info_kasan(void *mem, struct slab *slab);
*/
void slab_shrink_caches(unsigned long target_freep);

__END_CDECLS

#endif
Loading

0 comments on commit cea0cc9

Please sign in to comment.