summaryrefslogtreecommitdiff
path: root/kernel/include
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-04-08 10:39:48 -0400
committerFreya Murphy <freya@freyacat.org>2025-04-08 10:39:48 -0400
commit8a19547957a86bed3f58c9abc1ac218d04698faf (patch)
treeed7ccc6f3a8902915dfe6c9bf763fc45d752b3c4 /kernel/include
parentfmt (diff)
downloadcomus-8a19547957a86bed3f58c9abc1ac218d04698faf.tar.gz
comus-8a19547957a86bed3f58c9abc1ac218d04698faf.tar.bz2
comus-8a19547957a86bed3f58c9abc1ac218d04698faf.zip
break apart c libaray
Diffstat (limited to 'kernel/include')
-rw-r--r--kernel/include/comus/drivers/tty.h2
l---------kernel/include/comus/error.h1
-rw-r--r--kernel/include/comus/fs.h5
-rw-r--r--kernel/include/comus/memory.h54
-rw-r--r--kernel/include/comus/time.h39
-rw-r--r--kernel/include/lib.h20
-rw-r--r--kernel/include/lib/kctype.h22
-rw-r--r--kernel/include/lib/kio.h91
-rw-r--r--kernel/include/lib/klib.h196
-rw-r--r--kernel/include/lib/kstring.h154
10 files changed, 579 insertions, 5 deletions
diff --git a/kernel/include/comus/drivers/tty.h b/kernel/include/comus/drivers/tty.h
index 4f8a5f1..55aec64 100644
--- a/kernel/include/comus/drivers/tty.h
+++ b/kernel/include/comus/drivers/tty.h
@@ -17,6 +17,6 @@ void tty_out(char c);
/**
* Output a string to the terminal
*/
-void tty_out_str(char *str);
+void tty_out_str(const char *str);
#endif /* tty.h */
diff --git a/kernel/include/comus/error.h b/kernel/include/comus/error.h
new file mode 120000
index 0000000..9367cab
--- /dev/null
+++ b/kernel/include/comus/error.h
@@ -0,0 +1 @@
+../../../user/include/error.h \ No newline at end of file
diff --git a/kernel/include/comus/fs.h b/kernel/include/comus/fs.h
index 67c74e3..fe335ca 100644
--- a/kernel/include/comus/fs.h
+++ b/kernel/include/comus/fs.h
@@ -11,7 +11,10 @@
#include <stdint.h>
#include <stddef.h>
-#include <limits.h>
+
+// FIXME: aaaa
+#define MAX_DISKS 8
+#define MAX_FILE_NAME_LEN 256
struct disk {
/// set to 1 in array to state that fs is defined
diff --git a/kernel/include/comus/memory.h b/kernel/include/comus/memory.h
index 411c039..294c795 100644
--- a/kernel/include/comus/memory.h
+++ b/kernel/include/comus/memory.h
@@ -13,6 +13,7 @@
#include <stddef.h>
#define MMAP_MAX_ENTRY 64
+#define PAGE_SIZE 4096
struct memory_segment {
uint64_t addr;
@@ -54,14 +55,39 @@ uint64_t memory_used(void);
* @param writable - if this memory should be writable
* @param user - if this memory should be user writable
*/
-void *mapaddr(void *addr, size_t len);
+void *kmapaddr(void *addr, size_t len);
/**
* Unmaps mapped address from the mmap function
* @param addr - the address returned from mmap
* @param len - the length allocated
*/
-void unmapaddr(void *addr);
+void kunmapaddr(void *addr);
+
+/**
+ * Allocates size_t bytes in memory
+ *
+ * @param size - the amount of bytes to allocate
+ * @returns the address allocated or NULL on failure
+ */
+void *kalloc(size_t size);
+
+/**
+ * Rellocates a given allocated ptr to a new size of bytes in memory.
+ * If ptr is NULL it will allocate new memory.
+ *
+ * @param ptr - the pointer to reallocate
+ * @param size - the amount of bytes to reallocate to
+ * @returns the address allocated or NULL on failure
+ */
+void *krealloc(void *ptr, size_t size);
+
+/**
+ * Frees an allocated pointer in memory
+ *
+ * @param ptr - the pointer to free
+ */
+void kfree(void *ptr);
/**
* Attemps to load a mapped but not yet allocated page.
@@ -70,6 +96,28 @@ void unmapaddr(void *addr);
*
* @returns 0 on success and a negative error code on failure.
*/
-int load_page(void *virt_addr);
+int kload_page(void *virt_addr);
+
+/**
+ * Allocate a single page of memory
+ *
+ * @returns the address allocated or NULL on failure
+ */
+void *kalloc_page(void);
+
+/**
+ * Allocate size_t amount of contiguous virtual pages
+ *
+ * @param count - the number of pages to allocate
+ * @returns the address allocated or NULL on failure
+ */
+void *kalloc_pages(size_t count);
+
+/**
+ * Free allocated pages.
+ *
+ * @param ptr - the pointer provided by alloc_page or alloc_pages
+ */
+void kfree_pages(void *ptr);
#endif /* memory.h */
diff --git a/kernel/include/comus/time.h b/kernel/include/comus/time.h
new file mode 100644
index 0000000..6ecb2b7
--- /dev/null
+++ b/kernel/include/comus/time.h
@@ -0,0 +1,39 @@
+/**
+ * @file time.h
+ *
+ * @author Freya Murphy <freya@freyacat.org>
+ *
+ * System time structure
+ */
+
+#ifndef _TIME_H
+#define _TIME_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+struct time {
+ int sec; /// Seconds [0,59]
+ int min; /// Minutes [0,59]
+ int hour; /// Hour [0,23]
+ int mday; /// Day of month [1,31]
+ int mon; /// Month of year [0,11]
+ int year; /// Years since 1900
+ int wday; /// Day of week [0,6] (Sunday = 0)
+ int yday; /// Day of year [0,365]
+ int yn; /// Year number [0,99]
+ int cen; /// Century [19,20]
+ int leap; /// If year is a leap year (True == 1)
+};
+
+/**
+ * Return the current time in the system
+ */
+void gettime(struct time *time);
+
+/**
+ * Return current UTC time
+ */
+uint64_t unixtime(void);
+
+#endif /* time.h */
diff --git a/kernel/include/lib.h b/kernel/include/lib.h
new file mode 100644
index 0000000..be4e739
--- /dev/null
+++ b/kernel/include/lib.h
@@ -0,0 +1,20 @@
+/**
+ * @file lib.h
+ *
+ * @author Freya Murphy <freya@freyacat.org>
+ *
+ * Kernel C Library
+ */
+
+#ifndef _LIB_H
+#define _LIB_H
+
+#include <stdint.h>
+#include <stddef.h>
+#include <stdbool.h>
+#include <lib/kctype.h>
+#include <lib/kio.h>
+#include <lib/klib.h>
+#include <lib/kstring.h>
+
+#endif /* lib.h */
diff --git a/kernel/include/lib/kctype.h b/kernel/include/lib/kctype.h
new file mode 100644
index 0000000..6d090c6
--- /dev/null
+++ b/kernel/include/lib/kctype.h
@@ -0,0 +1,22 @@
+/**
+ * @file kctype.h
+ *
+ * @author Freya Murphy <freya@freyacat.org>
+ *
+ * Kernel C type libaray functions
+ */
+
+#ifndef _KCTYPE_H
+#define _KCTYPE_H
+
+/**
+ * @returns 1 if c is a space
+ */
+int isspace(int c);
+
+/**
+ * @returns 1 if c is a digit (0 - 9)
+ */
+int isdigit(int c);
+
+#endif /* kctype.h */
diff --git a/kernel/include/lib/kio.h b/kernel/include/lib/kio.h
new file mode 100644
index 0000000..652a85b
--- /dev/null
+++ b/kernel/include/lib/kio.h
@@ -0,0 +1,91 @@
+/**
+ * @file kio.h
+ *
+ * @author Freya Murphy <freya@freyacat.org>
+ *
+ * Kernel I/O definitions.
+ */
+
+#ifndef _KIO_H
+#define _KIO_H
+
+#include <stddef.h>
+#include <stdarg.h>
+
+/**
+ * Prints out a char
+ *
+ * @param c - the char
+ */
+void kputc(char c);
+
+/**
+ * Prints out a null terminated string
+ *
+ * @param s - the string
+ */
+void kputs(const char *s);
+
+/**
+ * prints out a formatted string
+ *
+ * @param format - the format string
+ * @param ... - variable args for the format
+ */
+__attribute__((format(printf, 1, 2))) void kprintf(const char *format, ...);
+
+/**
+ * prints out a formatted string to a buffer
+ *
+ * @param s - the string to write to
+ * @param format - the format string
+ * @param ... - variable args for the format
+ * @returns number of bytes written
+ */
+__attribute__((format(printf, 2, 3))) size_t ksprintf(char *restrict s,
+ const char *format, ...);
+
+/**
+ * prints out a formatted string to a buffer with a given max length
+ *
+ * @param s - the string to write to
+ * @param maxlen - the max len of the buffer
+ * @param format - the format string
+ * @param ... - variable args for the format
+ * @returns number of bytes written
+ */
+__attribute__((format(printf, 3, 4))) size_t ksnprintf(char *restrict s,
+ size_t maxlen,
+ const char *format, ...);
+
+/**
+ * prints out a formatted string
+ *
+ * @param format - the format string
+ * @param args - variable arg list for the format
+ */
+void kvprintf(const char *format, va_list args);
+
+/**
+ * prints out a formatted string to a buffer
+ *
+ * @param s - the string to write to
+ * @param format - the format string
+ * @param args - variable arg list for the format
+ * @returns number of bytes written
+ */
+size_t kvsprintf(char *restrict s, const char *format, va_list args);
+
+/**
+ * prints out a formatted string to a buffer with a given max length
+ *
+ * @param s - the string to write to
+ * @param maxlen - the max len of the buffer
+ * @param format - the format string
+ * @param args - variable arg list for the format
+ * @returns number of bytes written
+ */
+size_t kvsnprintf(char *restrict s, size_t maxlen, const char *format,
+ va_list args);
+
+#endif /* kio.h */
diff --git a/kernel/include/lib/klib.h b/kernel/include/lib/klib.h
new file mode 100644
index 0000000..c67e57d
--- /dev/null
+++ b/kernel/include/lib/klib.h
@@ -0,0 +1,196 @@
+/**
+ * @file klib.h
+ *
+ * @author Freya Murphy <freya@freyacat.org>
+ *
+ * Kernel libaray functions
+ */
+
+#ifndef _KLIB_H
+#define _KLIB_H
+
+#include <stddef.h>
+
+/**
+ * converts single digit int to base 36
+ * @param i - int
+ * @returns c - base 36 char
+ */
+char itoc(int i);
+
+/**
+ * converts single base 36 chat into int
+ * @param c - base 36 char
+ * @returns i - int, or -1 if the char was invalid
+ */
+int ctoi(char c);
+
+/**
+ * Converts the initial portiion of the string pointed to by s to int.
+ * @param s - the string to convert
+ * @returns the number inside s or 0 on error
+ */
+int atoi(const char *s);
+
+/**
+ * Converts the initial portiion of the string pointed to by s to long.
+ * @param s - the string to convert
+ * @returns the number inside s or 0 on error
+ */
+long int atol(const char *s);
+
+/**
+ * Converts the initial portiion of the string pointed to by s to long long.
+ * @param s - the string to convert
+ * @returns the number inside s or 0 on error
+ */
+long long int atoll(const char *s);
+
+/**
+ * Converts a integer to asci inside a string with a given radix (base).
+ * @param n - the number to convert
+ * @param buffer - the string buffer
+ * @param radix - the base to convert
+ */
+char *itoa(int n, char *buffer, int radix);
+
+/**
+ * Converts a long to asci inside a string with a given radix (base).
+ * @param n - the number to convert
+ * @param buffer - the string buffer
+ * @param radix - the base to convert
+ */
+char *ltoa(long int n, char *buffer, int radix);
+
+/**
+ * Converts a long long to asci inside a string with a given radix (base).
+ * @param n - the number to conver
+ * @param buffer - the string buffer
+ * @param radix - the base to convert
+ */
+char *lltoa(long long int n, char *buffer, int radix);
+
+/**
+ * Converts a unsigned integer to asci inside a string with a given radix (base).
+ * @param n - the number to convert
+ * @param buffer - the string buffer
+ * @param radix - the base to convert
+ */
+char *utoa(unsigned int n, char *buffer, int radix);
+
+/**
+ * Converts a unsigned long to asci inside a string with a given radix (base).
+ * @param n - the number to convert
+ * @param buffer - the string buffer
+ * @param radix - the base to convert
+ */
+char *ultoa(unsigned long int n, char *buffer, int radix);
+
+/**
+ * Converts a unsigned long long to asci inside a string with a given radix (base).
+ * @param n - the number to conver
+ * @param buffer - the string buffer
+ * @param radix - the base to convert
+ */
+char *ulltoa(unsigned long long int n, char *buffer, int radix);
+
+/**
+ * Converts the string in str to an int value based on the given base.
+ * The endptr is updated to where the string was no longer valid.
+ * @param str - the string buffer
+ * @param endptr - the endptr
+ * @param base - the base to convert to
+ * @returns 0 on error or success, error if endptr is still equal to str
+ */
+int strtoi(const char *str, char **endptr, int base);
+
+/**
+ * Converts the string in str to a long value based on the given base.
+ * The endptr is updated to where the string was no longer valid.
+ * @param str - the string buffer
+ * @param endptr - the endptr
+ * @param base - the base to convert to
+ * @returns 0 on error or success, error if endptr is still equal to str
+ */
+long int strtol(const char *str, char **endptr, int base);
+
+/**
+ * Converts the string in str to a long long value based on the given base.
+ * The endptr is updated to where the string was no longer valid.
+ * @param str - the string buffer
+ * @param endptr - the endptr
+ * @param base - the base to convert to
+ * @returns 0 on error or success, error if endptr is still equal to str
+ */
+long long int strtoll(const char *str, char **endptr, int base);
+
+/**
+ * Converts the string in str to an unsigned int value based on the given base.
+ * The endptr is updated to where the string was no longer valid.
+ * @param str - the string buffer
+ * @param endptr - the endptr
+ * @param base - the base to convert to
+ * @returns 0 on error or success, error if endptr is still equal to str
+ */
+unsigned int strtoui(const char *str, char **endptr, int base);
+
+/**
+ * Converts the string in str to an unsigned long value based on the given base.
+ * The endptr is updated to where the string was no longer valid.
+ * @param str - the string buffer
+ * @param endptr - the endptr
+ * @param base - the base to convert to
+ * @returns 0 on error or success, error if endptr is still equal to str
+ */
+unsigned long int strtoul(const char *str, char **endptr, int base);
+
+/**
+ * Converts the string in str to an unsigned long long value based on the given base.
+ * The endptr is updated to where the string was no longer valid.
+ * @param str - the string buffer
+ * @param endptr - the endptr
+ * @param base - the base to convert to
+ * @returns 0 on error or success, error if endptr is still equal to str
+ */
+unsigned long long int strtoull(const char *str, char **endptr, int base);
+
+/**
+ * Converts a byte count to a human readable file size of at most four characters
+ * using binary suffixes.
+ *
+ * The following rules are applied:
+ * - If the byte count is less than 1024, the count is written in decimal
+ * and no suffix is applied
+ * - Otherwise, repeatedly divide by 1024 until the value is under 1000.
+ * - If the value has two or three decimal digits, print it followed by the
+ * approprate suffix.
+ * - If the value has one decimal digit, print it along with a single fractional
+ * digit. This also applies if the value is zero.
+ *
+ * @param bytes - the bytes to convert
+ * @param buf - a pointer to the buffer to store it in (which must be at least five
+ * bytes long)
+ * @returns - buf
+ */
+char *btoa(size_t bytes, char *buf);
+
+/**
+ * This function confines an argument within specified bounds.
+ *
+ * @param min - lower bound
+ * @param value - value to be constrained
+ * @param max - upper bound
+ *
+ * @returns the constrained value
+ */
+unsigned int bound(unsigned int min, unsigned int value, unsigned int max);
+
+/**
+ * Abort the kernel with a given message.
+ *
+ * @param format - the format string
+ * @param ... - variable args for the format
+ */
+__attribute__((noreturn)) void panic(const char *format, ...);
+
+#endif /* klib.h */
diff --git a/kernel/include/lib/kstring.h b/kernel/include/lib/kstring.h
new file mode 100644
index 0000000..50334b4
--- /dev/null
+++ b/kernel/include/lib/kstring.h
@@ -0,0 +1,154 @@
+/**
+ * @file kstring.h
+ *
+ * @author Freya Murphy <freya@freyacat.org>
+ *
+ * Kernel String libaray functions
+ */
+
+#ifndef _KSTRING_H
+#define _KSTRING_H
+
+#include <stddef.h>
+
+/**
+ * Compare the first n bytes (interpreted as unsigned char) of the memory areas s1 and s2
+ * @param s1 - the first memory area
+ * @param s2 - the second memory area
+ * @param n - the byte count
+ * @returns an interger less than, equal to, or greater than 0 if the first n bytes
+ * of s1 are less than, equal to, or greater than s2.
+ */
+int memcmp(const void *restrict s1, const void *restrict s2, size_t n);
+
+/**
+ * Copy the first n bytes from memory area src to memory area dest. The memory
+ * areas must not overlap.
+ * @param dest - the destination
+ * @param src - the source
+ * @param n - the byte count
+ * @returns a pointer to dest
+ */
+void *memcpy(void *restrict dest, const void *restrict src, size_t n);
+
+/**
+ * Copy the first n bytes from memory area src to memory area dest. The memory
+ * areas may overlap; memmove behaves as though the bytes are first copied to a
+ * temporary array.
+ * @param dest - the destination
+ * @param src - the source
+ * @param n - the byte count
+ * @returns a pointer to dest
+ */
+void *memmove(void *restrict dest, const void *restrict src, size_t n);
+
+/**
+ * Fill the first n bytes of the memory region dest with the constant byte c.
+ * @param dest - the destination
+ * @param c - the byte to write
+ * @param n - the byte count
+ * @returns a pointer to dest
+ */
+void *memset(void *restrict dest, int c, size_t n);
+
+/**
+ * Copy the first n bytes from memory area src to memory area dest. The memory
+ * areas must not overlap.
+ * @param dest - the destination
+ * @param src - the source
+ * @param n - the byte count
+ * @returns a pointer to dest
+ */
+volatile void *memcpyv(volatile void *restrict dest,
+ const volatile void *restrict src, size_t n);
+
+/**
+ * Copy the first n bytes from memory area src to memory area dest. The memory
+ * areas may overlap; memmove behaves as though the bytes are first copied to a
+ * temporary array.
+ * @param dest - the destination
+ * @param src - the source
+ * @param n - the byte count
+ * @returns a pointer to dest
+ */
+volatile void *memmovev(volatile void *restrict dest,
+ const volatile void *restrict src, size_t n);
+
+/**
+ * Fill the first n bytes of the memory region dest with the constant byte c.
+ * @param dest - the destination
+ * @param c - the byte to write
+ * @param n - the byte count
+ * @returns a pointer to dest
+ */
+volatile void *memsetv(volatile void *restrict dest, int c, size_t n);
+
+/**
+ * Calculates the length of the string pointed to by str, excluding
+ * the terminating null byte
+ * @param str - the string pointer
+ * @returns the length of the string in bytes
+ */
+size_t strlen(const char *str);
+
+/**
+ * Compare null terminated string s1 and s2. The comparison is done using
+ * unsigned characters.
+ * @param s1 - a pointer to the first string
+ * @param s2 - a pointer to the second string
+ * @returns an interger less than, equal to, or greater than 0 if s1 compares less
+ * than, equal to, or greater than s2
+ */
+int strcmp(const char *restrict s1, const char *restrict s2, size_t n);
+
+/**
+ * Compare at most the first n bytes of the strings s1 and s2. The comparison is
+ * done using unsigned characters.
+ * @param s1 - a pointer to the first string
+ * @param s2 - a pointer to the second string
+ * @param n - the maximum number of bytes
+ * @returns an interger less than, equal to, or greater than 0 if s1 compares less
+ * than, equal to, or greater than s2
+ */
+int strncmp(const char *restrict s1, const char *restrict s2, size_t n);
+
+/**
+ * Copies the string pointed to by src into the buffer pointer to by dest.
+ * The dest buffer must be long enough to hold src.
+ * @param dest - the destination
+ * @param src - the source
+ * @returns a pointer to dest
+ */
+char *strcpy(char *restrict dest, const char *restrict src);
+
+/**
+ * Copies the string pointed to by src into the buffer pointer to by dest.
+ * The dest buffer must be long enough to hold src or size n.
+ * @param dest - the destination
+ * @param src - the source
+ * @param n - the maximum number of bytes
+ * @returns a pointer to dest
+ */
+char *strncpy(char *restrict dest, const char *restrict src, size_t n);
+
+/**
+ * Copies the string pointed to by src into the buffer pointed to by dest.
+ * The dest buffer must be long enough to hold src.
+ * @param dest - the destination
+ * @param src - the source
+ * @param n - the maximum number of bytes
+ * @returns a pointer to the terminating null byte
+ */
+char *stpcpy(char *restrict dest, const char *restrict src);
+
+/**
+ * Copies the string pointed to by src into the buffer pointed to by dest.
+ * The dest buffer must be long enough to hold src or size n.
+ * @param dest - the destination
+ * @param src - the source
+ * @param n - the maximum number of bytes
+ * @returns a pointer to the byte after the last character copied
+ */
+char *stpncpy(char *restrict dest, const char *restrict src, size_t n);
+
+#endif /* kstring.h */