8000 Merge pull request #5 from havensjg/07-multiboot · havensjg/osdev-demos@4bf4e40 · GitHub
[go: up one dir, main page]

8000 Skip to content

Commit 4bf4e40

Browse files
authored
Merge pull request #5 from havensjg/07-multiboot
[07-multiboot] Merge with main
2 parents 0b6b94f + d244058 commit 4bf4e40

File tree

17 files changed

+1141
-0
lines changed

17 files changed

+1141
-0
lines changed

07-multiboot/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

07-multiboot/CMakeLists.txt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
cmake_minimum_required(VERSION 3.17.0)
2+
3+
# prevent cmake from making test executables??
4+
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
5+
6+
# set up i686-elf cross-compiler tools
7+
include(toolchain-i686-elf.cmake)
8+
9+
project(OSDEV)
10+
11+
# enable assembly
12+
enable_language(ASM)
13+
14+
# check that grub is installed
15+
find_program(GRUB_EXECUTABLE grub-mkrescue REQUIRED)
16+
17+
# directory/ies containing header files
18+
include_directories(include)
19+
20+
# set up iso file structure
21+
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/isodir)
22+
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/isodir/boot)
23+
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/isodir/boot/grub)
24+
25+
# iso file
26+
add_custom_target(livecd
27+
COMMAND ${GRUB_EXECUTABLE} -o ${CMAKE_CURRENT_BINARY_DIR}/myos.iso ${CMAKE_CURRENT_BINARY_DIR}/isodir
28+
VERBATIM
29+
)
30+
31+
# grub.cfg into isodir
32+
add_dependencies(livecd grub_cfg)
33+
add_custom_target(grub_cfg
34+
COMMAND ${CMAKE_COMMAND} -E copy
35+
${CMAKE_SOURCE_DIR}/src/grub.cfg
36+
${CMAKE_CURRENT_BINARY_DIR}/isodir/boot/grub/grub.cfg
37+
)
38+
39+
# myos.bin into isodir
40+
add_dependencies(livecd myos_bin)
41+
add_custom_target(myos_bin
42+
COMMAND ${CMAKE_COMMAND} -E copy
43+
${CMAKE_CURRENT_BINARY_DIR}/myos.bin
44+
${CMAKE_CURRENT_BINARY_DIR}/isodir/boot/myos.bin
45+
DEPENDS myos.bin
46+
)
47+
48+
# myos.bin
49+
file(GLOB C_SOURCES
50+
"include/*.h"
51+
"src/*.c"
52+
)
53+
set_source_files_properties(${C_SOURCES} PROPERTIES COMPILE_OPTIONS "-std=gnu99;-ffreestanding;-O2;-Wall;-Wextra")
54+
55+
file (GLOB ASM_SOURCES
56+
"src/*.s"
57+
)
58+
59+
add_executable(myos.bin
60+
${C_SOURCES}
61+
${ASM_SOURCES}
62+
)
63+
set_target_properties(myos.bin PROPERTIES LINK_DEPENDS ${CMAKE_SOURCE_DIR}/src/linker.ld)
64+
target_link_libraries(myos.bin gcc)
65+
target_link_options(myos.bin PUBLIC -ffreestanding -O2 -nostdlib -T ${CMAKE_SOURCE_DIR}/src/linker.ld)

07-multiboot/include/io.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <stdint.h>
2+
3+
/* x86 outb instruction */
4+
static inline void outb(uint16_t port, uint8_t val)
5+
{
6+
asm volatile ( "outb %0, %1" : : "a"(val), "Nd"(port) );
7+
/* There's an outb %al, $imm8 encoding, for compile-time constant port numbers that fit in 8b. (N constraint).
8+
* Wider immediate constants would be truncated at assemble-time (e.g. "i" constraint).
9+
* The outb %al, %dx encoding is the only option for all other cases.
10+
* %1 expands to %dx because port is a uint16_t. %w1 could be used if we had the port number a wider C type */
11+
}
12+
13+
/* x86 inb instruction */
14+
static inline uint8_t inb(uint16_t port)
15+
{
16+
uint8_t ret;
17+
asm volatile ( "inb %1, %0"
18+
: "=a"(ret)
19+
: "Nd"(port) );
20+
return ret;
21+
}
22+
23+
/* x86 outw instruction */
24+
static inline void outw(uint16_t port, uint16_t val)
25+
{
26+
asm volatile ( "outw %0, %1" : : "a"(val), "Nd"(port) );
27+
}
28+
29+
/* x86 inw instruction */
30+
static inline uint16_t inw(uint16_t port)
31+
{
32+
uint16_t ret;
33+
asm volatile ( "inw %1, %0"
34+
: "=a"(ret)
35+
: "Nd"(port) );
36+
return ret;
37+
}
38+
39+
/* x86 outl instruction */
40+
static inline void outl(uint16_t port, uint32_t val)
41+
{
42+
asm volatile ( "outl %0, %1" : : "a"(val), "Nd"(port) );
43+
}
44+
45+
/* x86 inl instruction */
46+
static inline uint32_t inl(uint16_t port)
47+
{
48+
uint32_t ret;
49+
asm volatile ( "inl %1, %0"
50+
: "=a"(ret)
51+
: "Nd"(port) );
52+
return ret;
53+
}

0 commit comments

Comments
 (0)
0