Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -2581,7 +2581,7 @@ INCLUDE_FILE_PATTERNS =
# recursively expanded use the := operator instead of the = operator.
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.

PREDEFINED = __counted_by(x)= __sized_by(x)=
PREDEFINED = __counted_by(x)= __sized_by(x)= restrict=

# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this
# tag can be used to specify a list of macro names that should be expanded. The
Expand Down
7 changes: 7 additions & 0 deletions docs/doxygen/groups.dox
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@
@defgroup kinit Kernel initialization
@defgroup hal Hardware abstraction layer
@defgroup dt Device tree
@defgroup stdbigos
@defgroup sbi
@defgroup csr
@defgroup trap
@defgroup types
@defgroup string
@defgroup bitutils
*/
2 changes: 1 addition & 1 deletion docs/source/pages/api/kernel/mm/palloc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Physical memory allocator
=========================

.. doxygengroup:: palloc
.. doxygengroup:: palloc
2 changes: 1 addition & 1 deletion docs/source/pages/api/kernel/mm/pmm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Physical memory manager
==============================

.. doxygengroup:: pmm
.. doxygengroup:: pmm
2 changes: 1 addition & 1 deletion docs/source/pages/api/libs/dt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Device Tree API
===============

.. doxygengroup:: dt
.. doxygengroup:: dt
1 change: 1 addition & 0 deletions docs/source/pages/api/libs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Bigos Libraries
:maxdepth: 1

dt
stdbigos/index
5 changes: 5 additions & 0 deletions docs/source/pages/api/libs/stdbigos/bitutils.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
========
Bitutils
========

.. doxygengroup:: bitutils
5 changes: 5 additions & 0 deletions docs/source/pages/api/libs/stdbigos/csr.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
===
CSR
===

.. doxygengroup:: csr
13 changes: 13 additions & 0 deletions docs/source/pages/api/libs/stdbigos/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
========
Stdbigos
========

.. toctree::
:maxdepth: 1

sbi
csr
trap
types
string
bitutils
5 changes: 5 additions & 0 deletions docs/source/pages/api/libs/stdbigos/sbi.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
===
SBI
===

.. doxygengroup:: sbi
5 changes: 5 additions & 0 deletions docs/source/pages/api/libs/stdbigos/string.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
======
String
======

.. doxygengroup:: string
5 changes: 5 additions & 0 deletions docs/source/pages/api/libs/stdbigos/trap.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
====
Trap
====

.. doxygengroup:: trap
5 changes: 5 additions & 0 deletions docs/source/pages/api/libs/stdbigos/types.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
=====
Types
=====

.. doxygengroup:: types
6 changes: 3 additions & 3 deletions include/stdbigos/address.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _STDBIGOS_ADDRESS_H
#define _STDBIGOS_ADDRESS_H
#ifndef STDBIGOS_ADDRESS
#define STDBIGOS_ADDRESS

#ifdef __clang__
#define __noderef __attribute__((noderef))
Expand All @@ -13,4 +13,4 @@
#define __iomem
#endif

#endif
#endif // !STDBIGOS_ADDRESS
6 changes: 3 additions & 3 deletions include/stdbigos/array_sizes.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _STDBIGOS_ARRAY_SIZES_H
#define _STDBIGOS_ARRAY_SIZES_H
#ifndef STDBIGOS_ARRAY_SIZES
#define STDBIGOS_ARRAY_SIZES

#if __has_attribute(__counted_by__)
#define __counted_by(member) __attribute__((__counted_by__(member)))
Expand All @@ -13,4 +13,4 @@
#define __sized_by(member)
#endif

#endif
#endif // !STDBIGOS_ARRAY_SIZES
16 changes: 13 additions & 3 deletions include/stdbigos/bitutils.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
#ifndef _STDBIGOS_BITUTILS_H
#define _STDBIGOS_BITUTILS_H
#ifndef STDBIGOS_BITUTILS
#define STDBIGOS_BITUTILS

#include <stdbigos/types.h>

/// @addtogroup stdbigos
/// @{
/// @addtogroup bitutils
/// @{

u32 read_be32(const void* addr);

u64 read_be64(const void* addr);

u32 read_le32(const void* addr);

u64 read_le64(const void* addr);

u32 align_u32(u32 num, u32 align);

#endif
/// @}
/// @}

#endif // !STDBIGOS_BITUTILS
22 changes: 11 additions & 11 deletions include/stdbigos/buffer.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#ifndef _STDBIGOS_BUFFER_H
#define _STDBIGOS_BUFFER_H
#ifndef STDBIGOS_BUFFER
#define STDBIGOS_BUFFER

#include <stdbigos/array_sizes.h>
#include <stdbigos/types.h>

/// @addtogroup stdbigos
/// @{
/// @addtogroup buffer
/// @{

typedef struct buffer_t {
size_t size;
const void* data __sized_by(size);
} buffer_t;

// Helpers to create buffers
[[nodiscard]]
static inline buffer_t make_buffer(const void* data, size_t size) {
buffer_t buf = {.data = data, .size = size};
Expand All @@ -26,31 +30,24 @@ static inline bool buffer_is_empty(buffer_t buf) {
return !buffer_is_valid(buf) || buf.size == 0;
}

// Read big-endian 32-bit from buffer at given offset
[[nodiscard]]
bool buffer_read_u32_be(buffer_t buf, size_t offset, u32* out);

// Read big-endian 64-bit from buffer at given offset
[[nodiscard]]
bool buffer_read_u64_be(buffer_t buf, size_t offset, u64* out);

// Read little-endian 32-bit from buffer at given offset
[[nodiscard]]
bool buffer_read_u32_le(buffer_t buf, size_t offset, u32* out);

// Read little-endian 64-bit from buffer at given offset
[[nodiscard]]
bool buffer_read_u64_le(buffer_t buf, size_t offset, u64* out);

// Read a zero-terminated C-string from buf at offset and output it's length
[[nodiscard]]
bool buffer_read_cstring_len(buffer_t buf, size_t offset, const char** out_str, u64* len);

// Read a zero-terminated C-string from buf at offset
[[nodiscard]]
bool buffer_read_cstring(buffer_t buf, size_t offset, const char** out_str);

// Sub buffer
[[nodiscard]]
buffer_t buffer_sub_buffer(buffer_t buf, size_t offset, size_t max_size);

Expand All @@ -60,4 +57,7 @@ int buffer_memcmp(buffer_t lhs, buffer_t rhs);
[[nodiscard]]
bool buffer_equal(buffer_t lhs, buffer_t rhs);

#endif
/// @}
/// @}

#endif // !STDBIGOS_BUFFER
16 changes: 13 additions & 3 deletions include/stdbigos/csr.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _STDBIGOS_CSR_H_
#define _STDBIGOS_CSR_H_
#ifndef STDBIGOS_CSR
#define STDBIGOS_CSR

#include "types.h"

Expand Down Expand Up @@ -57,14 +57,24 @@
__asm__ volatile("csrc " #csr ", %0" : : "rK"(_val) : "memory"); \
})

/// @addtogroup stdbigos
/// @{
/// @addtogroup csr
/// @{

static inline u32 hartid() {
return CSR_READ_RELAXED(mhartid);
}

static inline void wfi() {
__asm__ volatile("wfi" ::: "memory");
}

static inline void ebreak() {
__asm__ volatile("ebreak" ::: "memory");
}

#endif
/// @}
/// @}

#endif // !STDBIGOS_CSR
5 changes: 3 additions & 2 deletions include/stdbigos/error.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef BIGOS_INCLUDE_STDBIGOS_ERROR
#define BIGOS_INCLUDE_STDBIGOS_ERROR
#ifndef STDBIGOS_ERROR
#define STDBIGOS_ERROR

/// @ingroup stdbigos error
typedef enum [[nodiscard]] {
ERR_NONE = 0,
ERR_NOT_IMPLEMENTED,
Expand Down
14 changes: 6 additions & 8 deletions include/stdbigos/math.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#ifndef _STDBIGOS_MATH_H
#define _STDBIGOS_MATH_H
#ifndef STDBIGOS_MATH
#define STDBIGOS_MATH

#define ALIGN_UP(x, align) (((x) + (align) - 1) / (align) * (align))
#define ALIGN_UP_POW2(x, pow2) (((x) + (1 << (pow2)) - 1) & ~((1 << (pow2)) - 1))
#define ALIGN_DOWN(x, align) (((x) / (align) * (align)))
#define ALIGN_DOWN_POW2(x, pow2) (((x) & ~((1 << pow2) - 1)))
#define ALIGN_DOWN(x, align) ((x) & ~((align) - 1))
#define ALIGN_UP(x, align) (ALIGN_DOWN((x) + (align) - 1, (align)))

#define POW2(x) (1 << (x))
#define EXP2(x) (1ull << (x))

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))

#endif
#endif // !STDBIGOS_MATH
112 changes: 112 additions & 0 deletions include/stdbigos/memory_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#ifndef STDBIGOS_MEMORY_TYPES
#define STDBIGOS_MEMORY_TYPES

#include <stdbigos/address.h>
#include <stdbigos/array_sizes.h>
#include <stdbigos/math.h>
#include <stdbigos/types.h>

/// @addtogroup stdbigos
/// @{
/// @addtogroup types
/// @{

/// Represents a contiguous range of valid, addressable memory.
typedef struct {
size_t size;
void* addr __sized_by(size);
} memory_region_t;

/// Represents a contiguous range of physical memory.
typedef struct {
size_t size;
__phys void* addr __sized_by(size);
} physical_memory_region_t;

/// Represents a range of memory, which isn't necessarily addressable.
typedef struct {
size_t size;
uintptr_t addr;
} memory_area_t;

[[nodiscard]]
static inline memory_area_t memory_region_to_area(memory_region_t reg) {
const memory_area_t out = {
.addr = (uintptr_t)reg.addr,
.size = reg.size,
};
return out;
}

[[nodiscard]]
static inline memory_area_t physical_memory_region_to_area(physical_memory_region_t reg) {
const memory_area_t out = {
.addr = (uintptr_t)reg.addr,
.size = reg.size,
};
return out;
}

[[nodiscard]]
static inline bool do_memory_areas_overlap(memory_area_t area1, memory_area_t area2) {
bool cond1 = area1.addr < area2.addr + area2.size;
bool cond2 = area2.addr < area1.addr + area1.size;
return cond1 && cond2;
}

[[nodiscard]]
static inline bool do_memory_regions_overlap(memory_region_t reg1, memory_region_t reg2) {
memory_area_t area1 = memory_region_to_area(reg1);
memory_area_t area2 = memory_region_to_area(reg2);
return do_memory_areas_overlap(area1, area2);
}

[[nodiscard]]
static inline memory_area_t memory_area_expand_to_alignment(memory_area_t area, size_t align) {
memory_area_t aligned_reg = {
.addr = ALIGN_DOWN(area.addr, align),
.size = area.size,
};
aligned_reg.size = area.size + area.addr - aligned_reg.addr;
aligned_reg.size = ALIGN_UP(aligned_reg.size, align);
return aligned_reg;
}

[[nodiscard]]
static inline memory_area_t memory_area_shrink_to_alignment(memory_area_t area, size_t align) {
memory_area_t aligned_reg = {
.addr = ALIGN_UP(area.addr, align),
.size = area.size,
};
aligned_reg.size = area.size - area.addr + aligned_reg.addr;
aligned_reg.size = ALIGN_DOWN(aligned_reg.size, align);
return aligned_reg;
}

[[nodiscard]]
static inline memory_region_t memory_region_shrink_to_alignment(memory_region_t region, size_t align) {
memory_area_t area = memory_region_to_area(region);
area = memory_area_shrink_to_alignment(area, align);
const memory_region_t out = {
.addr = (void*)area.addr,
.size = area.size,
};
return out;
}

[[nodiscard]]
static inline physical_memory_region_t physical_memory_region_shrink_to_alignment(physical_memory_region_t region,
size_t align) {
memory_area_t area = physical_memory_region_to_area(region);
area = memory_area_shrink_to_alignment(area, align);
const physical_memory_region_t out = {
.addr = (__phys void*)area.addr,
.size = area.size,
};
return out;
}

/// @}
/// @}

#endif // !STDBIGOS_MEMORY_TYPES
Loading
Loading