Style: harmonize formatting

Thanks to: "clang-format -i -style=file **/*.{c,h}"
This commit is contained in:
Mathieu Maret 2020-04-27 00:14:37 +02:00
parent fd6551e90c
commit 3b97d0307d
40 changed files with 1405 additions and 1458 deletions

View File

@ -31,7 +31,7 @@ int allocInit(void)
}
for (uint i = start; i <= SLUB_SIZE; i++) {
if ((ret = allocBookSlab(1U << i, 0))) {
if(ret == -EEXIST)
if (ret == -EEXIST)
continue;
pr_devel("Fail to allocBookSlab %d for %d \n", ret, (1U << i));
return ret;

View File

@ -20,4 +20,4 @@ int allocInit(void);
int allocBookSlab(size_t size, int selfContained);
void *malloc(size_t size);
void free(void* ptr);
void free(void *ptr);

View File

@ -1,22 +1,24 @@
#pragma once
#include "stack.h"
#include "klibc.h"
#include "stack.h"
#define assert(p) do { \
#define assert(p) \
do { \
if (!(p)) { \
printf("BUG at %s:%d assert(%s)\n", \
__FILE__, __LINE__, #p); \
printf("BUG at %s:%d assert(%s)\n", __FILE__, __LINE__, #p); \
printStackTrace(5); \
while(1){} \
while (1) { \
} \
} while (0)
} \
} while (0)
#define assertmsg(p, ...) do { \
#define assertmsg(p, ...) \
do { \
if (!(p)) { \
printf("BUG at %s:%d assert(%s)\n", \
__FILE__, __LINE__, #p); \
printf("BUG at %s:%d assert(%s)\n", __FILE__, __LINE__, #p); \
printf(__VA_ARGS__); \
printStackTrace(5); \
while(1){} \
while (1) { \
} \
} while (0)
} \
} while (0)

View File

@ -236,6 +236,7 @@ vaddr_t cpu_context_get_SP(const struct cpu_state *ctxt)
void cpu_context_dump(const struct cpu_state *ctxt)
{
printf("CPU: eip=%x esp=%x eflags=%x cs=%x ds=%x ss=%x err=%x", (unsigned)ctxt->eip,
(unsigned)ctxt, (unsigned)ctxt->eflags, (unsigned)GET_CPU_CS_REGISTER_VALUE(ctxt->cs),
(unsigned)ctxt->ds, (unsigned)ctxt->cpl0_ss, (unsigned)ctxt->error_code);
(unsigned)ctxt, (unsigned)ctxt->eflags,
(unsigned)GET_CPU_CS_REGISTER_VALUE(ctxt->cs), (unsigned)ctxt->ds,
(unsigned)ctxt->cpl0_ss, (unsigned)ctxt->error_code);
}

View File

@ -25,10 +25,9 @@
* be some kind of architecture-independent.
*/
#include "errno.h"
#include "stdarg.h"
#include "types.h"
#include "errno.h"
/**
* Opaque structure storing the CPU context of an inactive kernel or
@ -41,13 +40,11 @@
*/
struct cpu_state;
/**
* The type of the functions passed as arguments to the Kernel thread
* related functions.
*/
typedef void (cpu_kstate_function_arg1_t(void * arg1));
typedef void(cpu_kstate_function_arg1_t(void *arg1));
/**
* Function to create an initial context for a kernel thread starting
@ -83,14 +80,9 @@ typedef void (cpu_kstate_function_arg1_t(void * arg1));
*
* @note the newly created context is INTERRUPTIBLE by default !
*/
int cpu_kstate_init(struct cpu_state **kctxt,
cpu_kstate_function_arg1_t *start_func,
vaddr_t start_arg,
vaddr_t stack_bottom,
size_t stack_size,
cpu_kstate_function_arg1_t *exit_func,
vaddr_t exit_arg);
int cpu_kstate_init(struct cpu_state **kctxt, cpu_kstate_function_arg1_t *start_func,
vaddr_t start_arg, vaddr_t stack_bottom, size_t stack_size,
cpu_kstate_function_arg1_t *exit_func, vaddr_t exit_arg);
/**
* Function that performs an immediate context-switch from one
@ -103,9 +95,7 @@ int cpu_kstate_init(struct cpu_state **kctxt,
* @param to_ctxt The CPU will resume its execution with the struct
* cpu_state located at this address. Must NOT be NULL.
*/
void cpu_context_switch(struct cpu_state **from_ctxt,
struct cpu_state *to_ctxt);
void cpu_context_switch(struct cpu_state **from_ctxt, struct cpu_state *to_ctxt);
/*
* Switch to the new given context (of a kernel/user thread) without
@ -121,52 +111,43 @@ void cpu_context_switch(struct cpu_state **from_ctxt,
* called after having changed the stack, but before restoring the CPU
* context to switch_to_ctxt.
*/
void
cpu_context_exit_to(struct cpu_state *switch_to_ctxt,
cpu_kstate_function_arg1_t *reclaiming_func,
uint32_t reclaiming_arg) __attribute__((noreturn));
void cpu_context_exit_to(struct cpu_state *switch_to_ctxt,
cpu_kstate_function_arg1_t *reclaiming_func, uint32_t reclaiming_arg)
__attribute__((noreturn));
/* =======================================================================
* Public Accessor functions
*/
/**
* Return Program Counter stored in the saved kernel/user context
*/
vaddr_t cpu_context_get_PC(const struct cpu_state *ctxt);
/**
* Return Stack Pointer stored in the saved kernel/user context
*/
vaddr_t cpu_context_get_SP(const struct cpu_state *ctxt);
/**
* Dump the contents of the CPU context (bochs + x86_videomem)
*/
void cpu_context_dump(const struct cpu_state *ctxt);
/* =======================================================================
* Public Accessor functions TO BE USED ONLY BY Exception handlers
*/
/**
* Return the argument passed by the CPU upon exception, as stored in the
* saved context
*/
uint32_t cpu_context_get_EX_info(const struct cpu_state *ctxt);
/**
* Return the faulting address of the exception
*/
vaddr_t
cpu_context_get_EX_faulting_vaddr(const struct cpu_state *ctxt);
vaddr_t cpu_context_get_EX_faulting_vaddr(const struct cpu_state *ctxt);
/* =======================================================================
* Macros controlling stack poisoning.
@ -193,16 +174,14 @@ cpu_context_get_EX_faulting_vaddr(const struct cpu_state *ctxt);
/* #undef CPU_STATE_DETECT_KERNEL_STACK_OVERFLOW */
#if defined(CPU_STATE_DETECT_KERNEL_STACK_OVERFLOW)
void
cpu_state_prepare_detect_kernel_stack_overflow(const struct cpu_state *ctxt,
void cpu_state_prepare_detect_kernel_stack_overflow(const struct cpu_state *ctxt,
vaddr_t kernel_stack_bottom,
size_t kernel_stack_size);
void cpu_state_detect_kernel_stack_overflow(const struct cpu_state *ctxt,
vaddr_t kernel_stack_bottom,
size_t kernel_stack_size);
#else
# define cpu_state_prepare_detect_kernel_stack_overflow(ctxt,stkbottom,stksize) \
({ /* nop */ })
# define cpu_state_detect_kernel_stack_overflow(ctxt,stkbottom,stksize) \
({ /* nop */ })
#define cpu_state_prepare_detect_kernel_stack_overflow(ctxt, stkbottom, stksize) ({/* nop \
*/})
#define cpu_state_detect_kernel_stack_overflow(ctxt, stkbottom, stksize) ({/* nop */})
#endif

View File

@ -34,4 +34,3 @@
#define EPIPE 32 /* Broken pipe */
#define EDOM 33 /* Math argument out of domain of func */
#define ERANGE 34 /* Math result not representable */

View File

@ -17,8 +17,7 @@ int exceptionSetRoutine(int exception, exception_handler handler)
exception_handler_array[exception] = handler;
idt_set_handler(EXCEPTION_INTERRUPT_BASE_ADDRESS + exception, (unsigned int)handler,
0);
idt_set_handler(EXCEPTION_INTERRUPT_BASE_ADDRESS + exception, (unsigned int)handler, 0);
restore_IRQs(flags);
return 0;
}

View File

@ -41,6 +41,5 @@
#define EXCEPTION_NUM 32
typedef void (*exception_handler) (struct interrupt_frame *frame, ulong error_code);
typedef void (*exception_handler)(struct interrupt_frame *frame, ulong error_code);
int exceptionSetRoutine(int exception, exception_handler handler);

View File

@ -80,10 +80,9 @@ struct x86_gdt_register {
.limit_15_0 = 0xffff, \
.base_paged_addr_15_0 = 0, \
.base_paged_addr_23_16 = 0, \
.segment_type = \
((is_code) ? 0xb : 0x3), /* With descriptor_type (below) = 1 (code/data), \
* see Figure 3-1 of section 3.4.3.1 in Intel \
* x86 vol 3: \
.segment_type = ((is_code) ? 0xb : 0x3), /* With descriptor_type (below) = 1 \
* (code/data), see Figure 3-1 of \
* section 3.4.3.1 in Intel x86 vol 3: \
* - Code (bit 3 = 1): \
* bit 0: 1=Accessed \
* bit 1: 1=Readable \
@ -92,8 +91,8 @@ struct x86_gdt_register {
* bit 0: 1=Accessed \
* bit 1: 1=Writable \
* bit 2: 0=Expand up (stack-related) \
* For Conforming/non conforming segments, see \
* Intel x86 Vol 3 section 4.8.1.1 \
* For Conforming/non conforming segments, \
* see Intel x86 Vol 3 section 4.8.1.1 \
*/ \
.descriptor_type = 1, /* 1=Code/Data */ \
.dpl = ((descr_privilege_level)&0x3), \

View File

@ -34,4 +34,3 @@
* address space (ie "flat" virtual space).
*/
int gdtSetup(void);

View File

@ -38,8 +38,7 @@ struct idtRegister {
/* Build segment http://wiki.osdev.org/Selector*/
#define BUILD_SEGMENT_SELECTOR(desc_privilege, in_ldt, index) \
((((desc_privilege)&0x3) << 0) | (((in_ldt) ? 1 : 0) << 2) | \
((index) << 3))
((((desc_privilege)&0x3) << 0) | (((in_ldt) ? 1 : 0) << 2) | ((index) << 3))
int idtSetup();
int idt_set_handler(int index, unsigned int addr, int priviledge);

View File

@ -1,7 +1,6 @@
#pragma once
#include "stdarg.h"
// c.f. intel software-developer-vol-1 6.4.1
struct interrupt_frame {
uint32_t eip;

View File

@ -4,18 +4,17 @@
// NIH http://wiki.osdev.org/Inline_Assembly/Examples#I.2FO_access
static inline void outb(uint16_t port, uint8_t val)
{
asm volatile ( "outb %0, %1" : : "a"(val), "Nd"(port) );
/* There's an outb %al, $imm8 encoding, for compile-time constant port numbers that fit in 8b. (N constraint).
* Wider immediate constants would be truncated at assemble-time (e.g. "i" constraint).
* The outb %al, %dx encoding is the only option for all other cases.
* %1 expands to %dx because port is a uint16_t. %w1 could be used if we had the port number a wider C type */
asm volatile("outb %0, %1" : : "a"(val), "Nd"(port));
/* There's an outb %al, $imm8 encoding, for compile-time constant port numbers that fit in
* 8b. (N constraint). Wider immediate constants would be truncated at assemble-time (e.g.
* "i" constraint). The outb %al, %dx encoding is the only option for all other cases.
* %1 expands to %dx because port is a uint16_t. %w1 could be used if we had the port
* number a wider C type */
}
static inline uint8_t inb(uint16_t port)
{
uint8_t ret;
asm volatile ( "inb %1, %0"
: "=a"(ret)
: "Nd"(port) );
asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}

View File

@ -9,7 +9,9 @@ int irqSetup()
return 0;
}
irq_handler irq_handler_array[IRQ_NUM] = {NULL, };
irq_handler irq_handler_array[IRQ_NUM] = {
NULL,
};
int irqSetRoutine(int irq, irq_handler handler)
{
@ -22,8 +24,8 @@ int irqSetRoutine(int irq, irq_handler handler)
irq_handler_array[irq] = handler;
if (handler != NULL) {
int ret =
idt_set_handler(IRQ_INTERRUPT_BASE_ADDRESS + irq, (unsigned int)irq_handler_array[irq], 0);
int ret = idt_set_handler(IRQ_INTERRUPT_BASE_ADDRESS + irq,
(unsigned int)irq_handler_array[irq], 0);
if (!ret)
enableIrq(irq);
}

View File

@ -11,7 +11,6 @@
})
#define restore_IRQs(flags) restore_flags(flags)
#define IRQ_TIMER 0 // MASTER IRQ
#define IRQ_KEYBOARD 1
#define IRQ_SLAVE_PIC 2
@ -32,8 +31,9 @@
#define IRQ_INTERRUPT_BASE_ADDRESS 0x20
#define IRQ_NUM 16
// An handler should finish by the iret opcode -> https://wiki.osdev.org/Interrupt_Service_Routines
// That's why we use wrapper around them or the gcc interrupt attribut
// An handler should finish by the iret opcode ->
// https://wiki.osdev.org/Interrupt_Service_Routines That's why we use wrapper around them or
// the gcc interrupt attribut
// __attribute__((interrupt)) void (*irq_handler)(int irq);
typedef void (*irq_handler)(struct interrupt_frame *frame);
int irqSetup();

View File

@ -4,9 +4,9 @@
#define islower(c) (('a' <= (c)) && ((c) <= 'z'))
#define isupper(c) (('A' <= (c)) && ((c) <= 'Z'))
#define isdigit(c) (('0' <= (c)) && ((c) <= '9'))
#define isspace(c) (((c) == ' ') || ((c) == '\t') || \
((c) == '\f') || ((c) == '\n') || \
((c) == '\r') || ((c) == '\v'))
#define isspace(c) \
(((c) == ' ') || ((c) == '\t') || ((c) == '\f') || ((c) == '\n') || ((c) == '\r') || \
((c) == '\v'))
#define isprint(c) ((' ' <= (c)) && ((c) <= '~'))
int memcmp(const void *s1, const void *s2, size_t n);
@ -15,7 +15,7 @@ void *memset(void *s, int c, size_t n);
char *itoa(int value, char *str, int base);
void reverse(char s[]);
int strlen(const char s[]);
unsigned int strnlen(const char * s, size_t count);
unsigned int strnlen(const char *s, size_t count);
int strcmp(const char s1[], const char s2[]);
char *strzcpy(char *dst, const char *src, int len);
void puts(const char *str);
@ -29,19 +29,16 @@ void printf(const char *format, ...);
* gcc's format checking.
*/
#define no_printf(fmt, ...) \
({ \
({ \
if (0) \
printf(fmt, ##__VA_ARGS__); \
0; \
})
})
#ifdef DEBUG
#define pr_devel(fmt, ...) \
printf(pr_fmt(fmt), ##__VA_ARGS__)
#define pr_devel(fmt, ...) printf(pr_fmt(fmt), ##__VA_ARGS__)
#else
#define pr_devel(fmt, ...) \
no_printf(pr_fmt(fmt), ##__VA_ARGS__)
#define pr_devel(fmt, ...) no_printf(pr_fmt(fmt), ##__VA_ARGS__)
#endif
#define pr_info(fmt, ...) \
printf(pr_fmt(fmt), ##__VA_ARGS__)
#define pr_info(fmt, ...) printf(pr_fmt(fmt), ##__VA_ARGS__)

View File

@ -53,8 +53,8 @@ struct kthread *kthreadCreate(const char *name, cpu_kstate_function_arg1_t func,
else
strzcpy(thread->name, "[UNKNOW]", KTHREAD_NAME_MAX_LENGTH);
if (cpu_kstate_init(&thread->cpuState, (cpu_kstate_function_arg1_t *)func,
(vaddr_t)args, thread->stackAddr, thread->stackSize,
if (cpu_kstate_init(&thread->cpuState, (cpu_kstate_function_arg1_t *)func, (vaddr_t)args,
thread->stackAddr, thread->stackSize,
(cpu_kstate_function_arg1_t *)kthreadExit, 0))
goto free_mem;

View File

@ -25,170 +25,165 @@
* macros
*/
/* *_named are used when next and prev links are not exactly next
and prev. For instance when we have next_in_team, prev_in_team,
prev_global and next_global */
#define list_init_named(list,prev,next) \
((list) = NULL)
#define list_init_named(list, prev, next) ((list) = NULL)
#define list_singleton_named(list,item,prev,next) ({ \
#define list_singleton_named(list, item, prev, next) \
({ \
(item)->next = (item)->prev = (item); \
(list) = (item); \
})
})
#define list_is_empty_named(list,prev,next) \
((list) == NULL)
#define list_is_empty_named(list, prev, next) ((list) == NULL)
#define list_is_singleton_named(list,prev,next) \
( ((list) != NULL) && ((list)->prev == (list)->next) && ((list) == (list)->next) )
#define list_is_singleton_named(list, prev, next) \
(((list) != NULL) && ((list)->prev == (list)->next) && ((list) == (list)->next))
#define list_get_head_named(list,prev,next) \
(list)
#define list_get_head_named(list, prev, next) (list)
#define list_get_tail_named(list,prev,next) \
((list)?((list)->prev):NULL)
#define list_get_tail_named(list, prev, next) ((list) ? ((list)->prev) : NULL)
/* Internal macro : insert before the head == insert at tail */
#define __list_insert_atleft_named(before_this,item,prev,next) ({ \
#define __list_insert_atleft_named(before_this, item, prev, next) \
({ \
(before_this)->prev->next = (item); \
(item)->prev = (before_this)->prev; \
(before_this)->prev = (item); \
(item)->next = (before_this); \
})
})
/* @note Before_this and item are expected to be valid ! */
#define list_insert_before_named(list,before_this,item,prev,next) ({ \
__list_insert_atleft_named(before_this,item,prev,next); \
if ((list) == (before_this)) (list) = (item); \
})
#define list_insert_before_named(list, before_this, item, prev, next) \
({ \
__list_insert_atleft_named(before_this, item, prev, next); \
if ((list) == (before_this)) \
(list) = (item); \
})
/** @note After_this and item are expected to be valid ! */
#define list_insert_after_named(list,after_this,item,prev,next) ({ \
#define list_insert_after_named(list, after_this, item, prev, next) \
({ \
(after_this)->next->prev = (item); \
(item)->next = (after_this)->next; \
(after_this)->next = (item); \
(item)->prev = (after_this); \
})
})
#define list_add_head_named(list,item,prev,next) ({ \
#define list_add_head_named(list, item, prev, next) \
({ \
if (list) \
list_insert_before_named(list,list,item,prev,next); \
list_insert_before_named(list, list, item, prev, next); \
else \
list_singleton_named(list,item,prev,next); \
list_singleton_named(list, item, prev, next); \
(list) = (item); \
})
})
#define list_add_tail_named(list,item,prev,next) ({ \
#define list_add_tail_named(list, item, prev, next) \
({ \
if (list) \
__list_insert_atleft_named(list,item,prev,next); \
__list_insert_atleft_named(list, item, prev, next); \
else \
list_singleton_named(list,item,prev,next); \
})
list_singleton_named(list, item, prev, next); \
})
/** @note NO check whether item really is in list ! */
#define list_delete_named(list,item,prev,next) ({ \
if ( ((item)->next == (item)) && ((item)->prev == (item)) ) \
#define list_delete_named(list, item, prev, next) \
({ \
if (((item)->next == (item)) && ((item)->prev == (item))) \
(item)->next = (item)->prev = (list) = NULL; \
else { \
(item)->prev->next = (item)->next; \
(item)->next->prev = (item)->prev; \
if ((item) == (list)) (list) = (item)->next; \
if ((item) == (list)) \
(list) = (item)->next; \
(item)->prev = (item)->next = NULL; \
} \
})
})
#define list_pop_head_named(list,prev,next) ({ \
#define list_pop_head_named(list, prev, next) \
({ \
typeof(list) __ret_elt = (list); \
list_delete_named(list,__ret_elt,prev,next); \
__ret_elt; })
list_delete_named(list, __ret_elt, prev, next); \
__ret_elt; \
})
/** Loop statement that iterates through all of its elements, from
head to tail */
#define list_foreach_forward_named(list,iterator,nb_elements,prev,next) \
for (nb_elements=0, (iterator) = (list) ; \
(iterator) && (!nb_elements || ((iterator) != (list))) ; \
nb_elements++, (iterator) = (iterator)->next )
#define list_foreach_forward_named(list, iterator, nb_elements, prev, next) \
for (nb_elements = 0, (iterator) = (list); \
(iterator) && (!nb_elements || ((iterator) != (list))); \
nb_elements++, (iterator) = (iterator)->next)
/** Loop statement that iterates through all of its elements, from
tail back to head */
#define list_foreach_backward_named(list,iterator,nb_elements,prev,next) \
for (nb_elements=0, (iterator) = list_get_tail_named(list,prev,next) ; \
(iterator) && (!nb_elements || \
((iterator) != list_get_tail_named(list,prev,next))) ; \
nb_elements++, (iterator) = (iterator)->prev )
#define list_foreach_backward_named(list, iterator, nb_elements, prev, next) \
for (nb_elements = 0, (iterator) = list_get_tail_named(list, prev, next); \
(iterator) && \
(!nb_elements || ((iterator) != list_get_tail_named(list, prev, next))); \
nb_elements++, (iterator) = (iterator)->prev)
#define list_foreach_named list_foreach_forward_named
/** True when we exitted early from the foreach loop (ie break) */
#define list_foreach_early_break(list,iterator,nb_elements) \
((list) && ( \
((list) != (iterator)) || \
( ((list) == (iterator)) && (nb_elements == 0)) ))
#define list_foreach_early_break(list, iterator, nb_elements) \
((list) && (((list) != (iterator)) || (((list) == (iterator)) && (nb_elements == 0))))
/** Loop statement that also removes the item at each iteration. The
body of the loop is allowed to delete the iterator element from
memory. */
#define list_collapse_named(list,iterator,prev,next) \
for ( ; ({ ((iterator) = (list)) ; \
if (list) list_delete_named(list,iterator,prev,next) ; \
(iterator); }) ; )
#define list_collapse_named(list, iterator, prev, next) \
for (; ({ \
((iterator) = (list)); \
if (list) \
list_delete_named(list, iterator, prev, next); \
(iterator); \
});)
/*
* the same macros : assume that the prev and next fields are really
* named "prev" and "next"
*/
#define list_init(list) \
list_init_named(list,prev,next)
#define list_init(list) list_init_named(list, prev, next)
#define list_singleton(list,item) \
list_singleton_named(list,item,prev,next)
#define list_singleton(list, item) list_singleton_named(list, item, prev, next)
#define list_is_empty(list) \
list_is_empty_named(list,prev,next)
#define list_is_empty(list) list_is_empty_named(list, prev, next)
#define list_is_singleton(list) \
list_is_singleton_named(list,prev,next)
#define list_is_singleton(list) list_is_singleton_named(list, prev, next)
#define list_get_head(list) \
list_get_head_named(list,prev,next) \
#define list_get_head(list) list_get_head_named(list, prev, next)
#define list_get_tail(list) \
list_get_tail_named(list,prev,next) \
#define list_get_tail(list) list_get_tail_named(list, prev, next)
/* @note Before_this and item are expected to be valid ! */
#define list_insert_after(list,after_this,item) \
list_insert_after_named(list,after_this,item,prev,next)
#define list_insert_after(list, after_this, item) \
list_insert_after_named(list, after_this, item, prev, next)
/* @note After_this and item are expected to be valid ! */
#define list_insert_before(list,before_this,item) \
list_insert_before_named(list,before_this,item,prev,next)
#define list_insert_before(list, before_this, item) \
list_insert_before_named(list, before_this, item, prev, next)
#define list_add_head(list,item) \
list_add_head_named(list,item,prev,next)
#define list_add_head(list, item) list_add_head_named(list, item, prev, next)
#define list_add_tail(list,item) \
list_add_tail_named(list,item,prev,next)
#define list_add_tail(list, item) list_add_tail_named(list, item, prev, next)
/* @note NO check whether item really is in list ! */
#define list_delete(list,item) \
list_delete_named(list,item,prev,next)
#define list_delete(list, item) list_delete_named(list, item, prev, next)
#define list_pop_head(list) \
list_pop_head_named(list,prev,next)
#define list_pop_head(list) list_pop_head_named(list, prev, next)
#define list_foreach_forward(list,iterator,nb_elements) \
list_foreach_forward_named(list,iterator,nb_elements,prev,next)
#define list_foreach_forward(list, iterator, nb_elements) \
list_foreach_forward_named(list, iterator, nb_elements, prev, next)
#define list_foreach_backward(list,iterator,nb_elements) \
list_foreach_backward_named(list,iterator,nb_elements,prev,next)
#define list_foreach_backward(list, iterator, nb_elements) \
list_foreach_backward_named(list, iterator, nb_elements, prev, next)
#define list_foreach list_foreach_forward
#define list_collapse(list,iterator) \
list_collapse_named(list,iterator,prev,next)
#define list_collapse(list, iterator) list_collapse_named(list, iterator, prev, next)
#endif /* _SOS_LIST_H_ */

View File

@ -26,14 +26,14 @@ void cpuid(int code, uint32_t *a, uint32_t *d)
asm volatile("cpuid" : "=a"(*a), "=d"(*d) : "0"(code) : "ebx", "ecx");
}
void idleThread(void *arg){
void idleThread(void *arg)
{
(void)arg;
int count = 0;
while(1)
while (1)
printIntDetails(count++, GREEN, BLACK, 0, VGA_HEIGHT - 1);
}
// Multiboot information available here :
// https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#kernel_002ec
// https://www.gnu.org/software/grub/manual/multiboot/html_node/Boot-information-format.html#Boot%20information%20format
@ -112,8 +112,8 @@ void kmain(unsigned long magic, unsigned long addr)
printf("Enabling HW interrupts\n");
exceptionSetRoutine(EXCEPTION_DOUBLE_FAULT, ACCESS_INTERRUPT(EXCEPTION_DOUBLE_FAULT));
exceptionSetRoutine(EXCEPTION_DIVIDE_ZERO, ACCESS_INTERRUPT(EXCEPTION_DIVIDE_ZERO));
//Used by the DBG
//exceptionSetRoutine(EXCEPTION_DEBUG, ACCESS_INTERRUPT(EXCEPTION_DEBUG));
// Used by the DBG
// exceptionSetRoutine(EXCEPTION_DEBUG, ACCESS_INTERRUPT(EXCEPTION_DEBUG));
exceptionSetRoutine(EXCEPTION_NMI, ACCESS_INTERRUPT(EXCEPTION_NMI));
exceptionSetRoutine(EXCEPTION_BREAKPOINT, ACCESS_INTERRUPT(EXCEPTION_BREAKPOINT));
exceptionSetRoutine(EXCEPTION_OVERFLOW, ACCESS_INTERRUPT(EXCEPTION_OVERFLOW));

View File

@ -1,11 +1,9 @@
#include "math.h"
inline uint32_t log2(const uint32_t x) {
inline uint32_t log2(const uint32_t x)
{
uint32_t y;
// Get the highest set bit
asm ( "\tbsr %1, %0\n"
: "=r"(y)
: "r" (x)
);
asm("\tbsr %1, %0\n" : "=r"(y) : "r"(x));
return y;
}

View File

@ -1,7 +1,7 @@
#include "mem.h"
#include "klibc.h"
#include "list.h"
#include "types.h"
#include "klibc.h"
static struct mem_desc *page_desc = (struct mem_desc *)&__ld_kernel_end;
static struct mem_desc *free_page;
@ -17,11 +17,10 @@ int memSetup(paddr_t upperMem, paddr_t *lastUsedOut)
upperMem = ALIGN_DOWN(upperMem, PAGE_SIZE / 1024);
printf("Available Mem from 0x%x to 0x%x: %dMB \n", &__ld_kernel_end, upperMem * 1024,
(upperMem * 1024 - (uint32_t)&__ld_kernel_end) / (1024*1024));
(upperMem * 1024 - (uint32_t)&__ld_kernel_end) / (1024 * 1024));
// Memory description is stored after the kernel. We need some place to store it
unsigned long memdesc_end =
(unsigned long)page_desc +
((upperMem) / (PAGE_SIZE / 1024)) * sizeof(struct mem_desc);
(unsigned long)page_desc + ((upperMem) / (PAGE_SIZE / 1024)) * sizeof(struct mem_desc);
uint lastUsed = (memdesc_end >> PAGE_SHIFT) + 1;
list_init(free_page);
list_init(used_page);

View File

@ -1,7 +1,6 @@
#pragma once
#include "types.h"
#include "stdarg.h"
#include "types.h"
#define PAGE_SHIFT 12U
#define PAGE_SIZE (1U << PAGE_SHIFT)
@ -10,13 +9,12 @@
extern uint32_t __ld_kernel_begin;
extern uint32_t __ld_kernel_end;
struct mem_desc{
struct mem_desc {
paddr_t phy_addr;
long ref;
struct mem_desc *next, *prev;
};
int memSetup(paddr_t upperMem, paddr_t *lastUsed);
paddr_t allocPhyPage(void);
int unrefPhyPage(paddr_t addr);

View File

@ -131,11 +131,10 @@ int pageMap(vaddr_t vaddr, paddr_t paddr, int flags)
uint ptEntry = (vaddr >> PT_SHIFT) & PTE_MASK;
// Thank to mirroring, we can access the PD
struct pde *pd = (struct pde *)((PD_MIRROR_PAGE_IDX << PD_SHIFT) +
(PD_MIRROR_PAGE_IDX << PT_SHIFT));
struct pde *pd =
(struct pde *)((PD_MIRROR_PAGE_IDX << PD_SHIFT) + (PD_MIRROR_PAGE_IDX << PT_SHIFT));
struct pte *pt =
(struct pte *)((PD_MIRROR_PAGE_IDX << PD_SHIFT) + (pdEntry << PT_SHIFT));
struct pte *pt = (struct pte *)((PD_MIRROR_PAGE_IDX << PD_SHIFT) + (pdEntry << PT_SHIFT));
if (!pd[pdEntry].present) {
paddr_t ptPhy = allocPhyPage();
@ -177,11 +176,10 @@ int pageUnmap(vaddr_t vaddr)
uint ptEntry = (vaddr >> PT_SHIFT) & PTE_MASK;
// Thank to mirroring, we can access the PD
struct pde *pd = (struct pde *)((PD_MIRROR_PAGE_IDX << PD_SHIFT) +
(PD_MIRROR_PAGE_IDX << PT_SHIFT));
struct pde *pd =
(struct pde *)((PD_MIRROR_PAGE_IDX << PD_SHIFT) + (PD_MIRROR_PAGE_IDX << PT_SHIFT));
struct pte *pt =
(struct pte *)((PD_MIRROR_PAGE_IDX << PD_SHIFT) + (pdEntry << PT_SHIFT));
struct pte *pt = (struct pte *)((PD_MIRROR_PAGE_IDX << PD_SHIFT) + (pdEntry << PT_SHIFT));
if (!pd[pdEntry].present)
return -EINVAL;
if (!pt[ptEntry].present)

View File

@ -40,15 +40,11 @@
#define SEG_KCODE 1 /* Kernel code segment */
#define SEG_KDATA 2 /* Kernel data segment */
/**
* Helper macro that builds a segment register's value
*/
#define BUILD_SEGMENT_REG_VALUE(desc_privilege,in_ldt,seg_index) \
( (((desc_privilege) & 0x3) << 0) \
| (((in_ldt)?1:0) << 2) \
| ((seg_index) << 3) )
#define BUILD_SEGMENT_REG_VALUE(desc_privilege, in_ldt, seg_index) \
((((desc_privilege)&0x3) << 0) | (((in_ldt) ? 1 : 0) << 2) | ((seg_index) << 3))
/*
* Local segment selectors (LDT) for SOS/x86

View File

@ -1,6 +1,6 @@
#include "stack.h"
#include "types.h"
#include "klibc.h"
#include "types.h"
void printStackTrace(unsigned int maxFrames)
{
@ -24,8 +24,7 @@ void printStackTrace(unsigned int maxFrames)
printf("[%d] 0x%x (", frame, eip);
int nbArg = 0;
do {
if ((_stack_bottom <= (vaddr_t)arguments) &&
((vaddr_t)arguments <= _stack_top)) {
if ((_stack_bottom <= (vaddr_t)arguments) && ((vaddr_t)arguments <= _stack_top)) {
printf(" 0x%x", *arguments);
arguments += 1;
} else {

View File

@ -35,8 +35,8 @@ typedef __u64 uint64_t;
typedef __u64 u_int64_t;
typedef __s64 int64_t;
typedef enum { FALSE=0, TRUE } bool_t;
#define NULL ((void*)0)
typedef enum { FALSE = 0, TRUE } bool_t;
#define NULL ((void *)0)
#if __x86_64__
typedef unsigned long size_t;
@ -51,4 +51,4 @@ typedef unsigned int uintptr_t;
typedef void *va_list;
#define va_start(v, l) ((v) = (va_list) & (l) + sizeof(l))
#define va_end(v) ((v) = NULL)
#define va_arg(v, type) (*(type *)(((v)+=sizeof(type))-sizeof(type)))
#define va_arg(v, type) (*(type *)(((v) += sizeof(type)) - sizeof(type)))

View File

@ -1,31 +1,31 @@
#pragma once
#define USHRT_MAX ((u16)(~0U))
#define SHRT_MAX ((s16)(USHRT_MAX>>1))
#define SHRT_MAX ((s16)(USHRT_MAX >> 1))
#define SHRT_MIN ((s16)(-SHRT_MAX - 1))
#define INT_MAX ((int)(~0U>>1))
#define INT_MAX ((int)(~0U >> 1))
#define INT_MIN (-INT_MAX - 1)
#define UINT_MAX (~0U)
#define LONG_MAX ((long)(~0UL>>1))
#define LONG_MAX ((long)(~0UL >> 1))
#define LONG_MIN (-LONG_MAX - 1)
#define ULONG_MAX (~0UL)
#define LLONG_MAX ((long long)(~0ULL>>1))
#define LLONG_MAX ((long long)(~0ULL >> 1))
#define LLONG_MIN (-LLONG_MAX - 1)
#define ULLONG_MAX (~0ULL)
#define SIZE_MAX (~(size_t)0)
#define PHYS_ADDR_MAX (~(phys_addr_t)0)
#define U8_MAX ((u8)~0U)
#define S8_MAX ((s8)(U8_MAX>>1))
#define S8_MAX ((s8)(U8_MAX >> 1))
#define S8_MIN ((s8)(-S8_MAX - 1))
#define U16_MAX ((u16)~0U)
#define S16_MAX ((s16)(U16_MAX>>1))
#define S16_MAX ((s16)(U16_MAX >> 1))
#define S16_MIN ((s16)(-S16_MAX - 1))
#define U32_MAX ((u32)~0U)
#define S32_MAX ((s32)(U32_MAX>>1))
#define S32_MAX ((s32)(U32_MAX >> 1))
#define S32_MIN ((s32)(-S32_MAX - 1))
#define U64_MAX ((u64)~0ULL)
#define S64_MAX ((s64)(U64_MAX>>1))
#define S64_MAX ((s64)(U64_MAX >> 1))
#define S64_MIN ((s64)(-S64_MAX - 1))
#define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a)-1)

View File

@ -1,6 +1,6 @@
#include "keyboard.h"
#include "klibc.h"
#include "io.h"
#include "klibc.h"
const char *scancode[128] = {
/* 0 */ 0,
@ -271,7 +271,8 @@ void keyboard_do_irq()
unsigned char c = inb(KEYBOARD_DATA_PORT);
const char *key = NULL;
if (c > 0) {
if (c == 0xE0) { // non printable such as page up/down ... See https://wiki.osdev.org/Keyboard
if (c == 0xE0) { // non printable such as page up/down ... See
// https://wiki.osdev.org/Keyboard
isExt = 1;
} else if (isExt) {
isExt = 0;
@ -310,4 +311,3 @@ void keyboard_do_irq()
if (key)
printf(key);
}

View File

@ -14,7 +14,6 @@
#define PIC_SLAVE_DATA 0xa1
#define PIC_EOI 0x20 /* End-of-interrupt command code */
void EOIIrq(int irq);
void initPic(void);
void enableIrq(int irq);

View File

@ -13,4 +13,3 @@
// 3 -> low then high 3-1: mode. See https://wiki.osdev.org/PIT
int pitSetup(unsigned int freq);

View File

@ -28,7 +28,8 @@ void serialSetup(int speed)
outb(PORT + 0, div & 0xFF); // Set divisor lo byte
outb(PORT + 1, div >> 8); // Set divisor hi byte
outb(PORT + 3,
UART_NO_PARITY | UART_8BITS_WORD | UART_1_STOP_BIT); // 8 bits, no parity, one stop bit
UART_NO_PARITY | UART_8BITS_WORD |
UART_1_STOP_BIT); // 8 bits, no parity, one stop bit
outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
irqSetRoutine(IRQ_COM1, serial_handler);

View File

@ -36,9 +36,8 @@ void clearScreenLine(uint bgColor, uint line)
void printIntDetails(int integer, uint color, uint bgColor, int startX, int startY)
{
char num[sizeof(int) *
3]; // int max is 2^(sizeof(int)*8) which is (2^3)^(sizeof(int)*8/3) =
// 8^(sizeof(int)*8/3) ~ 10^(sizeof(int)*8/3)
char num[sizeof(int) * 3]; // int max is 2^(sizeof(int)*8) which is (2^3)^(sizeof(int)*8/3)
// = 8^(sizeof(int)*8/3) ~ 10^(sizeof(int)*8/3)
int x = startX;
int i = 0, k = 0;
if (integer < 0) {
@ -68,8 +67,7 @@ void vgaScrollUp(void)
{
long int colorAttr = vgaBgColor << 12;
volatile short *vga = (short *)VGA_ADDR;
for (int i = 1; i < VGA_HEIGHT - 1;
i++) { // last line is status line. Do not scroll it
for (int i = 1; i < VGA_HEIGHT - 1; i++) { // last line is status line. Do not scroll it
memcpy((void *)&vga[VGA_WIDTH * (i - 1)], (void *)&vga[VGA_WIDTH * i],
VGA_WIDTH * sizeof(short));
}
@ -78,8 +76,6 @@ void vgaScrollUp(void)
}
}
void VGAputc(const char str)
{
if (str == '\n') {

View File

@ -32,4 +32,3 @@ void vgaScrollUp(void);
void cursorEnable(uint8_t cursor_start, uint8_t cursor_end);
void cursorDisable(void);
void cursorMove(int x, int y);

View File

@ -28,14 +28,12 @@ void testPhymem(void)
while ((page = list_pop_head(allocated_page_list)) != NULL) {
assertmsg(page->phy_addr == (ulong)freeCount, "page %d modified", page);
assertmsg(unrefPhyPage((ulong)page) >= 0, "Failed to free page %d\n",
(ulong)page);
assertmsg(unrefPhyPage((ulong)page) >= 0, "Failed to free page %d\n", (ulong)page);
freeCount++;
}
printf("%d pages freed\n", freeCount);
assertmsg((page = (struct mem_desc *)allocPhyPage()) != NULL,
"Cannot allocate memory\n");
assertmsg((page = (struct mem_desc *)allocPhyPage()) != NULL, "Cannot allocate memory\n");
unrefPhyPage((ulong)page);
}
@ -113,17 +111,15 @@ static void testPaging(void)
printf("%d pages allocated\n", allocCount);
while ((page = list_pop_head(allocated_page_list)) != NULL) {
assertmsg((char)page->phy_addr == (char)freeCount,
"page modified %d but is %d\n", freeCount, page->phy_addr);
assertmsg(unrefPhyPage((ulong)page) >= 0, "Failed to free page %d\n",
(ulong)page);
assertmsg((char)page->phy_addr == (char)freeCount, "page modified %d but is %d\n",
freeCount, page->phy_addr);
assertmsg(unrefPhyPage((ulong)page) >= 0, "Failed to free page %d\n", (ulong)page);
pageUnmap((vaddr_t)page);
freeCount++;
}
printf("%d pages freed\n", freeCount);
assertmsg((page = (struct mem_desc *)allocPhyPage()) != NULL,
"Cannot allocate memory\n");
assertmsg((page = (struct mem_desc *)allocPhyPage()) != NULL, "Cannot allocate memory\n");
unrefPhyPage((ulong)page);
}
@ -155,14 +151,15 @@ struct cpu_state *ctxt_hello2;
struct cpu_state *ctxt_main;
vaddr_t hello1_stack, hello2_stack;
static void reclaim_stack(void * stack_vaddr)
static void reclaim_stack(void *stack_vaddr)
{
free(stack_vaddr);
}
static void exit_hello12(void * stack_vaddr)
static void exit_hello12(void *stack_vaddr)
{
cpu_context_exit_to(ctxt_main, (cpu_kstate_function_arg1_t *)reclaim_stack, (vaddr_t)stack_vaddr);
cpu_context_exit_to(ctxt_main, (cpu_kstate_function_arg1_t *)reclaim_stack,
(vaddr_t)stack_vaddr);
}
static void hello1(void *strIn)