Skip to content

Commit

Permalink
Reorganize and cleanup architecture-specific code (#83)
Browse files Browse the repository at this point in the history
* Organize architecture-specific code into subdirectories.
* Split per CPU data access and CPU feature detection into separate
files.
* Rename functions related to page mapping.
* Reorganize architecture-specific definitions in header files.
* Ensure object and dependency files don't conflict when assembly and C
source files have the same name (e.g. thread.c vs thread.asm).
  • Loading branch information
phaubertin authored Nov 13, 2024
1 parent 0f08b80 commit 13ce324
Show file tree
Hide file tree
Showing 79 changed files with 1,413 additions and 1,072 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-i686.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
steps:

- name: Install dependencies
run: sudo apt-get install -y gcc-multilib grub-common nasm qemu-system-x86 xorriso
run: sudo apt update && sudo apt-get install -y gcc-multilib grub-common nasm qemu-system-x86 xorriso

- uses: actions/checkout@v4
with:
Expand Down
36 changes: 18 additions & 18 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,27 @@ FORCE:

%.nasm: %.asm
@echo "; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" > $@
@echo "; * This file is automatically generated by passing the assembly language" >> $@
@echo "; * file through the C preprocessor. Any change made here will be lost" >> $@
@echo "; * on the next build." >> $@
@echo "; *" >> $@
@echo "; * The file you want to edit instead is $<." >> $@
@echo "; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" >> $@
@echo "" >> $@
$(CPP) $(DEPFLAGS) $(CPPFLAGS) -x assembler-with-cpp $< >> $@
@echo "; * This file is automatically generated by passing the assembly language" >> $@
@echo "; * file through the C preprocessor. Any change made here will be lost" >> $@
@echo "; * on the next build." >> $@
@echo "; *" >> $@
@echo "; * The file you want to edit instead is $<." >> $@
@echo "; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" >> $@
@echo "" >> $@
$(CPP) $(DEPFLAGS)$(basename $@)-nasm.d $(CPPFLAGS) -x assembler-with-cpp $< >> $@

%.ld: %.lds
@echo "/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" > $@
@echo " * This file is automatically generated by passing the linker script" >> $@
@echo " * through the C preprocessor. Any change made here will be lost on the" >> $@
@echo " * next build." >> $@
@echo " *" >> $@
@echo " * The file you want to edit instead is $<." >> $@
@echo " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */" >> $@
@echo "" >> $@
@echo "/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" > $@
@echo " * This file is automatically generated by passing the linker script" >> $@
@echo " * through the C preprocessor. Any change made here will be lost on the" >> $@
@echo " * next build." >> $@
@echo " *" >> $@
@echo " * The file you want to edit instead is $<." >> $@
@echo " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */" >> $@
@echo "" >> $@
$(CPP) $(CPPFLAGS) -x c -P $< >> $@

%.o: %.nasm
%-nasm.o: %.nasm
nasm -f elf $(NASMFLAGS) -o $@ $<

%.bin: %.nasm
Expand All @@ -81,4 +81,4 @@ FORCE:

.PRECIOUS: %.nasm

include $(wildcard $(patsubst %,%.d,$(basename $(objects))))
include $(wildcard $(depfiles))
14 changes: 10 additions & 4 deletions header.mk
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,23 @@ libjinue_utils = $(libjinue)/libjinue-utils.a

# object files
objects.c = $(sources.c:%.c=%.o)
objects.nasm = $(sources.nasm:%.asm=%.o)
objects.nasm = $(sources.nasm:%.asm=%-nasm.o)
objects.extra =
objects = $(objects.c) $(objects.nasm) $(objects.extra)

# Dependency files
depfiles.c = $(sources.c:%.c=%.d)
depfiles.nasm = $(sources.nasm:%.asm=%-nasm.d)
depfiles.extra =
depfiles = $(depfiles.c) $(depfiles.nasm) $(depfiles.extra)

# built targets
targets.phony ?=
targets ?= $(target)
targets.all = $(targets) $(targets.phony)

# files to clean up
unclean.build = $(objects) $(targets) $(objects:%.o=%.d) $(sources.nasm:%.asm=%.nasm)
unclean.build = $(objects) $(targets) $(depfiles) $(sources.nasm:%.asm=%.nasm)
unclean.extra =
unclean = $(unclean.build) $(unclean.extra)
unclean_recursive ?=
Expand Down Expand Up @@ -134,10 +140,10 @@ NASMFLAGS = $(NASMFLAGS.arch) $(NASMFLAGS.debug) $(NASMFLAGS.others)
STRIPFLAGS = --strip-debug

# Automatic dependencies generation flags
DEPFLAGS = -MT $@ -MD -MP -MF $*.d
DEPFLAGS = -MT $@ -MD -MP -MF

# Add dependencies generation flags when compiling object files from C source code
COMPILE.c = $(CC) $(DEPFLAGS) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
COMPILE.c = $(CC) $(DEPFLAGS) $(basename $@).d $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c

# macro to include common target definitions (all, clean, implicit rules)
common = $(jinue_root)/common.mk
2 changes: 1 addition & 1 deletion include/kernel/domain/alloc/slab.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#define JINUE_KERNEL_DOMAIN_SLAB_H

#include <kernel/machine/asm/machine.h>
#include <kernel/utils/vm.h>
#include <kernel/utils/pmap.h>
#include <kernel/types.h>

#define SLAB_SIZE PAGE_SIZE
Expand Down
64 changes: 64 additions & 0 deletions include/kernel/infrastructure/i686/asm/cpuid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2019-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_ASM_CPUID_H
#define JINUE_KERNEL_INFRASTRUCTURE_I686_ASM_CPUID_H

#define CPUID_FEATURE_FPU (1<<0)

#define CPUID_FEATURE_PSE (1<<3)

#define CPUID_FEATURE_PAE (1<<6)

#define CPUID_FEATURE_APIC (1<<9)

#define CPUID_FEATURE_SEP (1<<11)

#define CPUID_FEATURE_PGE (1<<13)

#define CPUID_FEATURE_CLFLUSH (1<<19)

#define CPUID_FEATURE_NXE (1<<20)

#define CPUID_FEATURE_HTT (1<<28)


#define CPUID_EXT_FEATURE_SYSCALL (1<<11)

#define CPUID_VENDOR_AMD_DW0 0x68747541 /* Auth */
#define CPUID_VENDOR_AMD_DW1 0x69746e65 /* enti */
#define CPUID_VENDOR_AMD_DW2 0x444d4163 /* cAMD */

#define CPUID_VENDOR_INTEL_DW0 0x756e6547 /* Genu */
#define CPUID_VENDOR_INTEL_DW1 0x49656e69 /* ineI */
#define CPUID_VENDOR_INTEL_DW2 0x6c65746e /* ntel */

#endif
61 changes: 61 additions & 0 deletions include/kernel/infrastructure/i686/asm/cpuinfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2019-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_ASM_CPUINFO_H
#define JINUE_KERNEL_INFRASTRUCTURE_I686_ASM_CPUINFO_H

/* features */

#define CPUINFO_FEATURE_CPUID (1<<0)

#define CPUINFO_FEATURE_SYSENTER (1<<1)

#define CPUINFO_FEATURE_SYSCALL (1<<2)

#define CPUINFO_FEATURE_LOCAL_APIC (1<<3)

#define CPUINFO_FEATURE_PAE (1<<4)

#define CPUINFO_FEATURE_PGE (1<<5)

#define CPUINFO_FEATURE_PSE (1<<6)

#define CPUINFO_FEATURE_NOEXEC (1<<7)

/* vendors */

#define CPUINFO_VENDOR_GENERIC 0

#define CPUINFO_VENDOR_AMD 1

#define CPUINFO_VENDOR_INTEL 2

#endif
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Philippe Aubertin.
* Copyright (C) 2019-2024 Philippe Aubertin.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -29,11 +29,9 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef JINUE_KERNEL_INFRASTRUCTURE_I686_REMAP_H
#define JINUE_KERNEL_INFRASTRUCTURE_I686_REMAP_H
#ifndef JINUE_KERNEL_INFRASTRUCTURE_I686_ASM_EFLAGS_H
#define JINUE_KERNEL_INFRASTRUCTURE_I686_ASM_EFLAGS_H

#include <kernel/infrastructure/i686/types.h>

void move_and_remap_kernel(addr_t end_addr, addr_t pte, uint32_t cr3_value);
#define CPU_EFLAGS_ID (1<<21)

#endif
50 changes: 50 additions & 0 deletions include/kernel/infrastructure/i686/asm/msr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2019-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_ASM_MSR_H
#define JINUE_KERNEL_INFRASTRUCTURE_I686_ASM_MSR_H

#define MSR_IA32_SYSENTER_CS 0x174

#define MSR_IA32_SYSENTER_ESP 0x175

#define MSR_IA32_SYSENTER_EIP 0x176

#define MSR_EFER 0xC0000080

#define MSR_STAR 0xC0000081


#define MSR_FLAG_EFER_SCE (1<<0)

#define MSR_FLAG_EFER_NXE (1<<11)

#endif
Loading

0 comments on commit 13ce324

Please sign in to comment.