From f3aea7e1bdccd3eacea2c6e49fbe6b7bbb5ae3cc Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 9 Oct 2022 04:02:41 +0300 Subject: [PATCH] CMOS clock --- .vscode/c_boilerplates.code-snippets | 1 - .vscode/c_cpp_properties.json | 1 + Kernel.cpp | 35 +++- arch/Time.cpp | 38 +++++ core/README.md | 11 ++ include/printf.h | 242 +++++++++++++-------------- include/time.hpp | 14 ++ 7 files changed, 213 insertions(+), 129 deletions(-) create mode 100644 arch/Time.cpp create mode 100644 core/README.md create mode 100644 include/time.hpp diff --git a/.vscode/c_boilerplates.code-snippets b/.vscode/c_boilerplates.code-snippets index 1ac15e7c..f0848f7c 100644 --- a/.vscode/c_boilerplates.code-snippets +++ b/.vscode/c_boilerplates.code-snippets @@ -1,6 +1,5 @@ { "Fennix Kernel Header": { - "scope": "c", "prefix": [ "head", ], diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 68d9df2f..3c76ec1e 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -6,6 +6,7 @@ "${workspaceFolder}/include/**" ], "defines": [ + "__debug_vscode__", "KERNEL_NAME=\"Fennix\"", "KERNEL_VERSION=\"1.0\"", "GIT_COMMIT=\"0000000000000000000000000000000000000000\"", diff --git a/Kernel.cpp b/Kernel.cpp index 66f52169..5d03f3c4 100644 --- a/Kernel.cpp +++ b/Kernel.cpp @@ -1,10 +1,34 @@ #include "kernel.h" +#include #include #include +#include +#include #include BootInfo *bInfo = nullptr; +Video::Display *Display = nullptr; + +// For the Display class. Printing on first buffer. +extern "C" void putchar(char c) { Display->Print(c, 0); } + +#ifdef __debug_vscode__ +extern "C" int printf_(const char *format, ...); +extern "C" int vprintf_(const char *format, va_list arg); +#endif + +void KPrint(const char *format, ...) +{ + Time tm = ReadClock(); + printf_("[%02ld:%02ld:%02ld] ", tm.Hour, tm.Minute, tm.Second); + va_list args; + va_start(args, format); + vprintf_(format, args); + va_end(args); + putchar('\n'); + Display->SetBuffer(0); +} EXTERNC void kernel_aarch64_entry(uint64_t dtb_ptr32, uint64_t x1, uint64_t x2, uint64_t x3) { @@ -20,11 +44,12 @@ EXTERNC void kernel_entry(BootInfo *Info) bInfo = (BootInfo *)KernelAllocator.RequestPages(TO_PAGES(sizeof(BootInfo))); memcpy(bInfo, Info, sizeof(BootInfo)); debug("BootInfo structure is at %p", bInfo); + Display = new Video::Display(bInfo->Framebuffer[0]); + printf_("%s - %s(%s)\n", KERNEL_NAME, KERNEL_VERSION, GIT_COMMIT_SHORT); + Display->SetBuffer(0); + for (size_t i = 0; i < 70; i++) + KPrint("Hello, World! (%ld)", i); + while (1) CPU::Halt(); } - -// TODO: Implement screen printing -extern "C" void putchar(int a, int b) -{ -} diff --git a/arch/Time.cpp b/arch/Time.cpp new file mode 100644 index 00000000..ec28c996 --- /dev/null +++ b/arch/Time.cpp @@ -0,0 +1,38 @@ +#include +#include + +Time ReadClock() +{ + Time tm; +#if defined(__amd64__) || defined(__i386__) + uint32_t t = 0; + outb(0x70, 0x00); + t = inb(0x71); + tm.Second = ((t & 0x0F) + ((t >> 4) * 10)); + outb(0x70, 0x02); + t = inb(0x71); + tm.Minute = ((t & 0x0F) + ((t >> 4) * 10)); + outb(0x70, 0x04); + t = inb(0x71); + tm.Hour = ((t & 0x0F) + ((t >> 4) * 10)); + outb(0x70, 0x07); + t = inb(0x71); + tm.Day = ((t & 0x0F) + ((t >> 4) * 10)); + outb(0x70, 0x08); + t = inb(0x71); + tm.Month = ((t & 0x0F) + ((t >> 4) * 10)); + outb(0x70, 0x09); + t = inb(0x71); + tm.Year = ((t & 0x0F) + ((t >> 4) * 10)); + tm.Counter = 0; +#elif defined(__aarch64__) + tm.Year = 0; + tm.Month = 0; + tm.Day = 0; + tm.Hour = 0; + tm.Minute = 0; + tm.Second = 0; + tm.Counter = 0; +#endif + return tm; +} diff --git a/core/README.md b/core/README.md new file mode 100644 index 00000000..df98851b --- /dev/null +++ b/core/README.md @@ -0,0 +1,11 @@ +# Core components + +This directory contains the core components of the project. These components are used by the kernel to provide the basic functionality of the operating system. + +--- + +## 💾 Memory + +Contains the memory management code. +It is responsible for allocating and freeing memory. +It also provides the `kmalloc`, `kcalloc`, `krealloc` and `kfree` functions that are used by the rest of the kernel. diff --git a/include/printf.h b/include/printf.h index c49d7dcb..9191a363 100644 --- a/include/printf.h +++ b/include/printf.h @@ -39,20 +39,21 @@ #ifndef PRINTF_H_ #define PRINTF_H_ -# include -# include +#include +#include #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif #ifdef __GNUC__ -# define ATTR_PRINTF(one_based_format_index, first_arg) \ -__attribute__((format(__printf__, (one_based_format_index), (first_arg)))) -# define ATTR_VPRINTF(one_based_format_index) ATTR_PRINTF((one_based_format_index), 0) +#define ATTR_PRINTF(one_based_format_index, first_arg) \ + __attribute__((format(__printf__, (one_based_format_index), (first_arg)))) +#define ATTR_VPRINTF(one_based_format_index) ATTR_PRINTF((one_based_format_index), 0) #else -# define ATTR_PRINTF((one_based_format_index), (first_arg)) -# define ATTR_VPRINTF(one_based_format_index) +#define ATTR_PRINTF((one_based_format_index), (first_arg)) +#define ATTR_VPRINTF(one_based_format_index) #endif #ifndef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES @@ -60,12 +61,12 @@ __attribute__((format(__printf__, (one_based_format_index), (first_arg)))) #endif #if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES -# define printf_ printf -# define sprintf_ sprintf -# define vsprintf_ vsprintf -# define snprintf_ snprintf -# define vsnprintf_ vsnprintf -# define vprintf_ vprintf +#define printf_ printf +#define sprintf_ sprintf +#define vsprintf_ vsprintf +#define snprintf_ snprintf +#define vsnprintf_ vsnprintf +#define vprintf_ vprintf #endif // If you want to include this implementation file directly rather than @@ -76,124 +77,119 @@ __attribute__((format(__printf__, (one_based_format_index), (first_arg)))) #define PRINTF_VISIBILITY #endif -/** - * Prints/send a single character to some opaque output entity - * - * @note This function is not implemented by the library, only declared; you must provide an - * implementation if you wish to use the @ref printf / @ref vprintf function (and possibly - * for linking against the library, if your toolchain does not support discarding unused functions) - * - * @note The output could be as simple as a wrapper for the `write()` system call on a Unix-like - * system, or even libc's @ref putchar , for replicating actual functionality of libc's @ref printf - * function; but on an embedded system it may involve interaction with a special output device, - * like a UART, etc. - * - * @note in libc's @ref putchar, the parameter type is an int; this was intended to support the - * representation of either a proper character or EOF in a variable - but this is really not - * meaningful to pass into @ref putchar and is discouraged today. See further discussion in: - * @link https://stackoverflow.com/q/17452847/1593077 - * - * @param c the single character to print - */ -PRINTF_VISIBILITY -void putchar(char c); + /** + * Prints/send a single character to some opaque output entity + * + * @note This function is not implemented by the library, only declared; you must provide an + * implementation if you wish to use the @ref printf / @ref vprintf function (and possibly + * for linking against the library, if your toolchain does not support discarding unused functions) + * + * @note The output could be as simple as a wrapper for the `write()` system call on a Unix-like + * system, or even libc's @ref putchar , for replicating actual functionality of libc's @ref printf + * function; but on an embedded system it may involve interaction with a special output device, + * like a UART, etc. + * + * @note in libc's @ref putchar, the parameter type is an int; this was intended to support the + * representation of either a proper character or EOF in a variable - but this is really not + * meaningful to pass into @ref putchar and is discouraged today. See further discussion in: + * @link https://stackoverflow.com/q/17452847/1593077 + * + * @param c the single character to print + */ + PRINTF_VISIBILITY + void putchar(char c); + /** + * An implementation of the C standard's printf/vprintf + * + * @note you must implement a @ref putchar_ function for using this function - it invokes @ref putchar_ + * rather than directly performing any I/O (which insulates it from any dependence on the operating system + * and external libraries). + * + * @param format A string specifying the format of the output, with %-marked specifiers of how to interpret + * additional arguments. + * @param arg Additional arguments to the function, one for each %-specifier in @p format string + * @return The number of characters written into @p s, not counting the terminating null character + */ + ///@{ + PRINTF_VISIBILITY + int printf_(const char *format, ...) ATTR_PRINTF(1, 2); + PRINTF_VISIBILITY + int vprintf_(const char *format, va_list arg) ATTR_VPRINTF(1); + ///@} -/** - * An implementation of the C standard's printf/vprintf - * - * @note you must implement a @ref putchar_ function for using this function - it invokes @ref putchar_ - * rather than directly performing any I/O (which insulates it from any dependence on the operating system - * and external libraries). - * - * @param format A string specifying the format of the output, with %-marked specifiers of how to interpret - * additional arguments. - * @param arg Additional arguments to the function, one for each %-specifier in @p format string - * @return The number of characters written into @p s, not counting the terminating null character - */ - ///@{ -PRINTF_VISIBILITY -int printf_(const char* format, ...) ATTR_PRINTF(1, 2); -PRINTF_VISIBILITY -int vprintf_(const char* format, va_list arg) ATTR_VPRINTF(1); -///@} + /** + * An implementation of the C standard's sprintf/vsprintf + * + * @note For security considerations (the potential for exceeding the buffer bounds), please consider using + * the size-constrained variant, @ref snprintf / @ref vsnprintf , instead. + * + * @param s An array in which to store the formatted string. It must be large enough to fit the formatted + * output! + * @param format A string specifying the format of the output, with %-marked specifiers of how to interpret + * additional arguments. + * @param arg Additional arguments to the function, one for each specifier in @p format + * @return The number of characters written into @p s, not counting the terminating null character + */ + ///@{ + PRINTF_VISIBILITY + int sprintf_(char *s, const char *format, ...) ATTR_PRINTF(2, 3); + PRINTF_VISIBILITY + int vsprintf_(char *s, const char *format, va_list arg) ATTR_VPRINTF(2); + ///@} + /** + * An implementation of the C standard's snprintf/vsnprintf + * + * @param s An array in which to store the formatted string. It must be large enough to fit either the + * entire formatted output, or at least @p n characters. Alternatively, it can be NULL, in which case + * nothing will be printed, and only the number of characters which _could_ have been printed is + * tallied and returned. + * @param n The maximum number of characters to write to the array, including a terminating null character + * @param format A string specifying the format of the output, with %-marked specifiers of how to interpret + * additional arguments. + * @param arg Additional arguments to the function, one for each specifier in @p format + * @return The number of characters that COULD have been written into @p s, not counting the terminating + * null character. A value equal or larger than @p n indicates truncation. Only when the returned value + * is non-negative and less than @p n, the null-terminated string has been fully and successfully printed. + */ + ///@{ + PRINTF_VISIBILITY + int snprintf_(char *s, size_t count, const char *format, ...) ATTR_PRINTF(3, 4); + PRINTF_VISIBILITY + int vsnprintf_(char *s, size_t count, const char *format, va_list arg) ATTR_VPRINTF(3); + ///@} -/** - * An implementation of the C standard's sprintf/vsprintf - * - * @note For security considerations (the potential for exceeding the buffer bounds), please consider using - * the size-constrained variant, @ref snprintf / @ref vsnprintf , instead. - * - * @param s An array in which to store the formatted string. It must be large enough to fit the formatted - * output! - * @param format A string specifying the format of the output, with %-marked specifiers of how to interpret - * additional arguments. - * @param arg Additional arguments to the function, one for each specifier in @p format - * @return The number of characters written into @p s, not counting the terminating null character - */ -///@{ -PRINTF_VISIBILITY -int sprintf_(char* s, const char* format, ...) ATTR_PRINTF(2, 3); -PRINTF_VISIBILITY -int vsprintf_(char* s, const char* format, va_list arg) ATTR_VPRINTF(2); -///@} - - -/** - * An implementation of the C standard's snprintf/vsnprintf - * - * @param s An array in which to store the formatted string. It must be large enough to fit either the - * entire formatted output, or at least @p n characters. Alternatively, it can be NULL, in which case - * nothing will be printed, and only the number of characters which _could_ have been printed is - * tallied and returned. - * @param n The maximum number of characters to write to the array, including a terminating null character - * @param format A string specifying the format of the output, with %-marked specifiers of how to interpret - * additional arguments. - * @param arg Additional arguments to the function, one for each specifier in @p format - * @return The number of characters that COULD have been written into @p s, not counting the terminating - * null character. A value equal or larger than @p n indicates truncation. Only when the returned value - * is non-negative and less than @p n, the null-terminated string has been fully and successfully printed. - */ -///@{ -PRINTF_VISIBILITY -int snprintf_(char* s, size_t count, const char* format, ...) ATTR_PRINTF(3, 4); -PRINTF_VISIBILITY -int vsnprintf_(char* s, size_t count, const char* format, va_list arg) ATTR_VPRINTF(3); -///@} - - - -/** - * printf/vprintf with user-specified output function - * - * An alternative to @ref printf_, in which the output function is specified dynamically - * (rather than @ref putchar_ being used) - * - * @param out An output function which takes one character and a type-erased additional parameters - * @param extra_arg The type-erased argument to pass to the output function @p out with each call - * @param format A string specifying the format of the output, with %-marked specifiers of how to interpret - * additional arguments. - * @param arg Additional arguments to the function, one for each specifier in @p format - * @return The number of characters for which the output f unction was invoked, not counting the terminating null character - * - */ -PRINTF_VISIBILITY -int fctprintf(void (*out)(char c, void* extra_arg), void* extra_arg, const char* format, ...) ATTR_PRINTF(3, 4); -PRINTF_VISIBILITY -int vfctprintf(void (*out)(char c, void* extra_arg), void* extra_arg, const char* format, va_list arg) ATTR_VPRINTF(3); + /** + * printf/vprintf with user-specified output function + * + * An alternative to @ref printf_, in which the output function is specified dynamically + * (rather than @ref putchar_ being used) + * + * @param out An output function which takes one character and a type-erased additional parameters + * @param extra_arg The type-erased argument to pass to the output function @p out with each call + * @param format A string specifying the format of the output, with %-marked specifiers of how to interpret + * additional arguments. + * @param arg Additional arguments to the function, one for each specifier in @p format + * @return The number of characters for which the output f unction was invoked, not counting the terminating null character + * + */ + PRINTF_VISIBILITY + int fctprintf(void (*out)(char c, void *extra_arg), void *extra_arg, const char *format, ...) ATTR_PRINTF(3, 4); + PRINTF_VISIBILITY + int vfctprintf(void (*out)(char c, void *extra_arg), void *extra_arg, const char *format, va_list arg) ATTR_VPRINTF(3); #if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES -# undef printf_ -# undef sprintf_ -# undef vsprintf_ -# undef snprintf_ -# undef vsnprintf_ -# undef vprintf_ +#undef printf_ +#undef sprintf_ +#undef vsprintf_ +#undef snprintf_ +#undef vsnprintf_ +#undef vprintf_ #endif #ifdef __cplusplus } #endif -#endif // PRINTF_H_ \ No newline at end of file +#endif // PRINTF_H_ \ No newline at end of file diff --git a/include/time.hpp b/include/time.hpp new file mode 100644 index 00000000..fc92c9b3 --- /dev/null +++ b/include/time.hpp @@ -0,0 +1,14 @@ +#ifndef __FENNIX_KERNEL_TIME_H__ +#define __FENNIX_KERNEL_TIME_H__ + +#include + +struct Time +{ + uint64_t Year, Month, Day, Hour, Minute, Second; + uint64_t Counter; +}; + +Time ReadClock(); + +#endif // !__FENNIX_KERNEL_TIME_H__