Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronization, part II #96

Merged
merged 5 commits into from
Dec 2, 2024
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
38 changes: 38 additions & 0 deletions include/kernel/infrastructure/i686/exports/spinlock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2024 Philippe Aubertin.
* All rights reserved.

* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the author nor the names of other contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef JINUE_KERNEL_INFRASTRUCTURE_I686_EXPORTS_SPINLOCK_H
#define JINUE_KERNEL_INFRASTRUCTURE_I686_EXPORTS_SPINLOCK_H

/** static initializer for a spinlock */
#define SPINLOCK_INITIALIZER { .lock = 0 }

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef JINUE_KERNEL_INFRASTRUCTURE_I686_EXPORTS_H
#define JINUE_KERNEL_INFRASTRUCTURE_I686_EXPORTS_H
#ifndef JINUE_KERNEL_INFRASTRUCTURE_I686_EXPORTS_TYPES_H
#define JINUE_KERNEL_INFRASTRUCTURE_I686_EXPORTS_TYPES_H

#include <kernel/typedeps.h>

Expand Down
2 changes: 1 addition & 1 deletion include/kernel/infrastructure/i686/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#define JINUE_KERNEL_INFRASTRUCTURE_I686_TYPES_H

#include <kernel/infrastructure/i686/asm/descriptors.h>
#include <kernel/infrastructure/i686/exports.h>
#include <kernel/infrastructure/i686/exports/types.h>
#include <kernel/machine/types.h>
#include <kernel/types.h>
#include <sys/elf.h>
Expand Down
6 changes: 4 additions & 2 deletions include/kernel/machine/spinlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@

#include <kernel/machine/types.h>

#define SPINLOCK_STATIC { .lock = 0 }
/* Must define SPINLOCK_INITIALIZER. */
#ifdef __i686__
#include <kernel/infrastructure/i686/exports/spinlock.h>
#endif

void init_spinlock(spinlock_t *lock);

Expand All @@ -43,4 +46,3 @@ void spin_lock(spinlock_t *lock);
void spin_unlock(spinlock_t *lock);

#endif

2 changes: 1 addition & 1 deletion include/kernel/machine/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#define JINUE_KERNEL_MACHINE_TYPES_H

#ifdef __i686__
#include <kernel/infrastructure/i686/exports.h>
#include <kernel/infrastructure/i686/exports/types.h>
#endif

#endif
2 changes: 1 addition & 1 deletion include/kernel/typedeps.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* <kernel/typedeps.h>
* ^ ^
* | |
* | e.g. <kernel/infrastructure/i686/exports.h> for i686
* | e.g. <kernel/infrastructure/i686/exports/types.h> for i686
* | ^
* | |
* | <kernel/machine/types.h>
Expand Down
2 changes: 1 addition & 1 deletion include/kernel/utils/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ typedef struct {

typedef list_node_t **list_cursor_t;

#define STATIC_LIST {.head = NULL, .tail = NULL}
#define LIST_INITIALIZER {.head = NULL, .tail = NULL}

static inline void init_list(list_t *list) {
list->head = NULL;
Expand Down
13 changes: 12 additions & 1 deletion kernel/domain/alloc/page_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <kernel/domain/alloc/vmalloc.h>
#include <kernel/machine/asm/machine.h>
#include <kernel/machine/pmap.h>
#include <kernel/machine/spinlock.h>
#include <string.h>


Expand All @@ -43,7 +44,9 @@ struct alloc_page {

static struct alloc_page *head_page = NULL;

unsigned int page_count = 0;
static unsigned int page_count = 0;

static spinlock_t alloc_lock;

/**
* Allocate a page of kernel memory.
Expand All @@ -55,13 +58,17 @@ unsigned int page_count = 0;
*
* */
void *page_alloc(void) {
spin_lock(&alloc_lock);

struct alloc_page *alloc_page = head_page;

if(alloc_page != NULL) {
head_page = alloc_page->next;
--page_count;
}

spin_unlock(&alloc_lock);

return alloc_page;
}

Expand All @@ -79,10 +86,14 @@ void *page_alloc(void) {
*
* */
void page_free(void *page) {
spin_lock(&alloc_lock);

struct alloc_page *alloc_page = page;
alloc_page->next = head_page;
head_page = alloc_page;
++page_count;

spin_unlock(&alloc_lock);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion kernel/domain/services/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,22 @@

#include <jinue/shared/asm/logging.h>
#include <kernel/domain/services/logging.h>
#include <kernel/machine/spinlock.h>
#include <kernel/utils/list.h>
#include <kernel/types.h>
#include <stdio.h>

list_t loggers = STATIC_LIST;
static list_t loggers = LIST_INITIALIZER;

static spinlock_t logging_spinlock = SPINLOCK_INITIALIZER;

void register_logger(logger_t *logger) {
list_enqueue(&loggers, &logger->loggers);
}

static void log_message(int loglevel, const char *restrict format, va_list args) {
spin_lock(&logging_spinlock);

/* TODO implement kernel logs ring buffer and get rid of this buffer
*
* This is static so it does not take a big chunk of the thread's stack. The
Expand All @@ -50,6 +55,8 @@ static void log_message(int loglevel, const char *restrict format, va_list args)

size_t length = vsnprintf(message, sizeof(message), format, args);
logging_add_message(loglevel, message, length);

spin_unlock(&logging_spinlock);
}

void logging_add_message(int loglevel, const char *message, size_t n) {
Expand Down
4 changes: 2 additions & 2 deletions kernel/domain/services/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ static struct {
list_t queue;
spinlock_t lock;
} ready_queue = {
.queue = STATIC_LIST,
.lock = SPINLOCK_STATIC
.queue = LIST_INITIALIZER,
.lock = SPINLOCK_INITIALIZER
};

/**
Expand Down