Skip to content

Commit

Permalink
refacto(utils: macro): add BIT macro to generate nth bit
Browse files Browse the repository at this point in the history
Changes:
* Renamed old BIT macro to BIT_READ
* Create new BIT macro to generate nth BIT (1 << n)
  • Loading branch information
d4ilyrun committed Feb 21, 2024
1 parent ab150c4 commit 3b9f18f
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion kernel/arch/i686/src/devices/pic.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static DEFINE_INTERRUPT_HANDLER(irq_keyboard)
const u8 scan_code = inb(0x60);

// If not key release; write character
if (!BIT(scan_code, 7) && ascii[scan_code])
if (!BIT_READ(scan_code, 7) && ascii[scan_code])
tty_putchar(ascii[scan_code]);

pic_eoi(IRQ_KEYBOARD);
Expand Down
4 changes: 2 additions & 2 deletions kernel/arch/i686/src/devices/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void uart_reset()
int uart_putc(const char c)
{
/* Wait until transfer buffer is empty */
WAIT_FOR(BIT(inb(UART_REG(LSR)), 5));
WAIT_FOR(BIT_READ(inb(UART_REG(LSR)), 5));

outb(UART_REG(THR), c);
return 0;
Expand All @@ -89,7 +89,7 @@ int uart_write(const char *buf, size_t length)
char uart_getc()
{
/* Wait until data is available to be read */
WAIT_FOR(BIT(inb(UART_REG(LSR)), 0));
WAIT_FOR(BIT_READ(inb(UART_REG(LSR)), 0));

return inb(UART_REG(THR));
}
Expand Down
2 changes: 2 additions & 0 deletions kernel/arch/i686/src/gdt.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ void gdt_init(void)
*
* To perform this operation, we assume that we are in protected mode,
* and that we are using a falt model (which is the case if using GRUB).
*
* FIXME: Should not use a fixed address
*/
static gdtr gdtr = {.size = GDT_SIZE - 1, .offset = GDT_BASE_ADDRESS};
ASM("lgdt (%0)" : : "m"(gdtr) : "memory");
Expand Down
4 changes: 1 addition & 3 deletions kernel/arch/i686/src/interrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ static const char *interrupt_names[] = {
};

// IDT entry flag: gate is present
#define IDT_PRESENT 0x80

// TODO: Find a way to inline this ...
#define IDT_PRESENT BIT(7)

const char *interrupts_to_str(u8 nr)
{
Expand Down
3 changes: 2 additions & 1 deletion kernel/include/kernel/pmm.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <kernel/memory.h>

#include <utils/macro.h>
#include <utils/types.h>

#include <multiboot.h>
Expand Down Expand Up @@ -45,7 +46,7 @@

/// The pageframe should be located inside the kernel's physical address space.
#define PMM_MAP_KERNEL_BIT 0x1
#define PMM_MAP_KERNEL (1 << PMM_MAP_KERNEL_BIT) // TODO: BIT macro
#define PMM_MAP_KERNEL BIT(PMM_MAP_KERNEL_BIT)

/// @}

Expand Down
9 changes: 6 additions & 3 deletions kernel/include/utils/macro.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#ifndef UTILS_MACRO_H
#define UTILS_MACRO_H

/* Generate the nth power of 2 (nth bit set) */
#define BIT(_n) (1 << (_n))

/* Clear the nth bit */
#define BIT_MASK(_x, _n) ((_x) & ~(1 << (_n)))
#define BIT_MASK(_x, _n) ((_x) & ~BIT((_n)))

/* Set the nth bit */
#define BIT_SET(_x, _n) ((_x) | (1 << (_n)))
#define BIT_SET(_x, _n) ((_x) | BIT((_n)))

/* Read the nth bit */
#define BIT(_x, _n) ((_x) & (1 << (_n)))
#define BIT_READ(_x, _n) ((_x)&BIT((_n)))

/* Most/Less significant byte from 16bit integer */
#define MSB(_x) ((_x) >> 8)
Expand Down
6 changes: 3 additions & 3 deletions kernel/src/memory/pmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ static inline void pmm_bitmap_set(u32 page, u8 availability)
/// @return a pageframe's state according to the allocator's bitmap
static inline int pmm_bitmap_read(u32 page)
{
return BIT(g_pmm_free_bitmap[BITMAP_INDEX(page)], page % 32);
return BIT_READ(g_pmm_free_bitmap[BITMAP_INDEX(page)], page % 32);
}

static bool pmm_initialize_bitmap(struct multiboot_info *mbt)
{
// If bit 6 in the flags uint16_t is set, then the mmap_* fields are valid
if (!BIT(mbt->flags, 6)) {
if (!BIT_READ(mbt->flags, 6)) {
log_err("PMM", "Multiboot structure does not support memory map.");
return false;
}
Expand Down Expand Up @@ -189,7 +189,7 @@ bool pmm_init(struct multiboot_info *mbt)

u32 pmm_allocate(int flags)
{
pmm_frame_allocator *allocator = BIT(flags, PMM_MAP_KERNEL_BIT)
pmm_frame_allocator *allocator = BIT_READ(flags, PMM_MAP_KERNEL_BIT)
? &g_pmm_kernel_allocator
: &g_pmm_user_allocator;

Expand Down

0 comments on commit 3b9f18f

Please sign in to comment.