Hello there,
I am trying to develop my own, simple operating system to learn more
about how kernels work and low level stuff like that. However, I am
stuck at setting up paging while switching long mode (64-bit protected
mode) in x86 processors.
Ar Rakin <rakinar2@onesoftnet.eu.org> writes:
Hello there,[67 lines deleted]
I am trying to develop my own, simple operating system to learn more
about how kernels work and low level stuff like that. However, I am
stuck at setting up paging while switching long mode (64-bit protected
mode) in x86 processors.
The assembly code I currently have:
[...]
I am not sure what is wrong, but when I run my kernel in
If anyone knows what is wrong with this code, please let me know. Any
help will be appreciated!
You're asking about x86 assembly language, not C, so you're not likely
to get help here. I'm not sure whether there's a newsgroup where your question would be on-topic. (comp.lang.asm.x86 appears to be dead.)
You might consider posting on Stack Overflow.
On 27/02/2025 16:57, Ar Rakin wrote:
Hello there,
I am trying to develop my own, simple operating system to learn more
about how kernels work and low level stuff like that. However, I am
stuck at setting up paging while switching long mode (64-bit protected
mode) in x86 processors.
Are you trying to learn about OS kernels, or about the complexities of low-level x86 stuff? Those are different things.
If you want to learn about how operating systems work, you might
consider starting on microcontrollers. There are lots of RTOS's
available to study, and it is much easier to understand what is going
on - there is none of the multi-layered disaster of x86 modes to deal
with.
Once you are happy with threads, context switches, locks, priorities,
and all the rest of that stuff, you can start moving up and adding
features for MMU's, SMP, and more, using bigger target processors.
On the other hand, if you are interested in learning the intricacies
of the x86 world, you need to look elsewhere for information - as
Keith says, it is not really C related when you are writing
assembly. comp.arch might be a helpful group.
However, it is possible to use C for almost all code here. But it
will not be standard C - it is not even standard free-standing C. You
will make use of compiler extensions, bits of inline assembly,
pre-main C code (so none of the library will be initialised, and program-lifetime data will not be set up), and other such highly
non-portable code. I remember reading once about LinuxBIOS (now
coreboot) having just four lines of assembly running out of reset,
before everything else was in C.
Hello there,
I am trying to develop my own, simple operating system to learn more
about how kernels work and low level stuff like that. However, I am
stuck at setting up paging while switching long mode (64-bit protected
mode) in x86 processors.
The assembly code I currently have:
#define PG_START 0x000000000
#define MSR_EFER 0xc0000080
.section .bss, "aw", @nobits
.align 4096
pml4_tbl:
.skip 4096
pdpt_tbl:
.skip 4096
.text
.globl _mboot_start
_mboot_start:
/* GRUB executes this code in 32-bit protected mode. */
/* Write (pdpt_tbl | 0x3) to the first 8 bytes of pml4_tbl */
movl $pdpt_tbl, %eax
orl $0x3, %eax
movl $pml4_tbl, %edi
movl %eax, (%edi)
xorl %eax, %eax
movl %eax, 4(%edi)
movl $pdpt_tbl, %edi
movl $PG_START, %eax
/* 0x83 = 0b10000011; flags: present, writable, upervisor-only,
1GB huge page */
movl $0x83, (%edi)
movl %eax, 4(%edi)
/* Enable Physical Address Extension (PAE) */
movl %cr4, %eax
btsl $5, %eax
movl %eax, %cr4
/* Load the address of the PML4 table into %cr3 */
movl $pml4_tbl, %edi
movl %edi, %cr3
/* Enable long mode */
movl $MSR_EFER, %ecx
rdmsr
btsl $8, %eax
wrmsr
/* Enable paging */
movl %cr0, %eax
btsl $31, %eax
movl %eax, %cr0
/* Jump to 64-bit code */
ljmpl $0x08, $long_mode_entry
.loop:
hlt
jmp .loop
long_mode_entry:
.code64
xorw %ax, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
movw %ax, %ss
callq kmain
callq kabort
I am not sure what is wrong, but when I run my kernel in
qemu-system-x86_64, it causes a triple fault when trying to jump to the
long mode code. After a lot of debugging, I am sure that the issue is
with paging, because removing the ljmpl and paging instructions do not
cause any further errors and the kernel runs fine in 32-bit mode.
If anyone knows what is wrong with this code, please let me know. Any
help will be appreciated!
On 28 Feb 2025 09:59, David Brown <david.brown@hesbynett.no> wrote:
[snip]
On the other hand, if you are interested in learning the intricacies
of the x86 world, you need to look elsewhere for information - as
Keith says, it is not really C related when you are writing
assembly. comp.arch might be a helpful group.
Looks like comp.arch might be the group I needed to find! Thanks.
[snip]
Also just to clarify, from what I know, since the x86 processors start
in 16-bit real mode for the sake of compatibility, you'd have to start >writing 16-bit code first, then switch to 32-bit protected mode, then
finally switch to 64-bit long mode. However if you use an existing >bootloader such as GRUB, then you usually would not need to worry about >16-bit code since GRUB drops you in 32-bit protected mode. But still,
if you wish to switch to 64-bit long mode, you *will* have to mix 64-bit
and 32-bit code together.
Either I don't know how to, or I just don't feel comfortable mixing
32-bit and 64-bit code together *when in C* because you will have a hard
time linking those together. Please correct me if I am wrong.
"David Brown" <david.brown@hesbynett.no> wrote in message >news:vprtt6$3jah9$1@dont-email.me...
On 27/02/2025 16:57, Ar Rakin wrote:
Do you consider the concept of a BIOS (as seen as the IBM PC),
"legitimate to use"
and do you consider MSDOS (which uses that
BIOS) to be an operating system?
There are no other active Usenet groups unfortunately. At least I don't
know any.
"Paul Edwards" <mutazilah@gmail.com> writes:
"David Brown" <david.brown@hesbynett.no> wrote in message >>news:vprtt6$3jah9$1@dont-email.me...
On 27/02/2025 16:57, Ar Rakin wrote:
Do you consider the concept of a BIOS (as seen as the IBM PC),
"legitimate to use"
In the abstract, possibly. But the last half century has
shown that BIOS as an I/O abstraction layer was a bad idea
from the start.
I am trying to develop my own, simple operating system to learn more
about how kernels work and low level stuff like that. However, I am
stuck at setting up paging while switching long mode (64-bit protected >mode) in x86 processors.
"Scott Lurndal" <scott@slp53.sl.home> wrote in message news:JdFwP.46247$SZca.36276@fx13.iad...
"Paul Edwards" <mutazilah@gmail.com> writes:
"David Brown" <david.brown@hesbynett.no> wrote in message
news:vprtt6$3jah9$1@dont-email.me...
On 27/02/2025 16:57, Ar Rakin wrote:
Do you consider the concept of a BIOS (as seen as the IBM PC),
"legitimate to use"
In the abstract, possibly. But the last half century has
shown that BIOS as an I/O abstraction layer was a bad idea
from the start.
and do you consider MSDOS (which uses that
BIOS) to be an operating system?
No, MSDOS was, is, will always be a simple program loader.
Plus manages memory.
Plus manages file systems
Plus provides an API
Plus provides a device driver interface
Plus provides a command shell
Plus provides other utilities
scott@slp53.sl.home (Scott Lurndal) writes:
"Paul Edwards" <mutazilah@gmail.com> writes:
"David Brown" <david.brown@hesbynett.no> wrote in message >>>news:vprtt6$3jah9$1@dont-email.me...
On 27/02/2025 16:57, Ar Rakin wrote:
Do you consider the concept of a BIOS (as seen as the IBM PC), >>>"legitimate to use"
In the abstract, possibly. But the last half century has
shown that BIOS as an I/O abstraction layer was a bad idea
from the start.
Would you elaborate or point out an article or book that could clarify
the ideas that have made you to make such remark? Sounds interesting.
Salvador Mirzo <smirzo@example.com> writes:
[snip]
Would you elaborate or point out an article or book that could clarify
the ideas that have made you to make such remark? Sounds interesting.
When the BIOS (as was originally intended) controls all the I/O interfaces, >it fundamentally limits access to new and experimental devices, and limits >the ability to generationally improve the hardware (e.g. Compare NVMe driver >interface with a legacy ISA device vis-a-vis the OS interface to the device).
It requires the mainboard manufacturer that provides the BIOS include
support for all new third-party hardware innovations. Given the BIOS
was orignally a ROM, such systems were extremely difficult to update
and there was no way to accomodate new third-party hardware without
bypassing the BIOS entirely.
The expansion ROM on PCI was an attempt to remedy this, but clearly
falls short when supporting multiple hardware families (e.g. various
RISC and CISC architectures) pushing the burden to support new
or different operating systems upon the device hardware manufacturer;
which either requires differnent SKUs for each generation of each
supported processor family, a significant burden on the device hardware >manufacturer.
BIOS is a least-common-denominator hardware abstraction interface.
"bart" <bc@freeuk.com> wrote in message news:vq208q$re74$1@dont-email.me...
On 02/03/2025 08:22, Paul Edwards wrote:
"Scott Lurndal" <scott@slp53.sl.home> wrote in message
news:JdFwP.46247$SZca.36276@fx13.iad...
"Paul Edwards" <mutazilah@gmail.com> writes:
"David Brown" <david.brown@hesbynett.no> wrote in message
news:vprtt6$3jah9$1@dont-email.me...
On 27/02/2025 16:57, Ar Rakin wrote:
Do you consider the concept of a BIOS (as seen as the IBM PC),
"legitimate to use"
In the abstract, possibly. But the last half century has
shown that BIOS as an I/O abstraction layer was a bad idea
from the start.
and do you consider MSDOS (which uses that
BIOS) to be an operating system?
No, MSDOS was, is, will always be a simple program loader.
Plus manages memory.
How does it do that? I seem to recall that you got 640KB minus whatever
the resident parts of the OS needed.
Yes - and? Then you need to manage that memory, so you
need a memory manager, which MSDOS provides. It's not
trivial to write/debug one of those either.
I'm not sure what your question is exactly, so I'll include code
to call the interrupt (21H AH=48H) you need to obtain memory.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 150:47:45 |
Calls: | 10,383 |
Files: | 14,054 |
Messages: | 6,417,791 |