Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #390518 > unrolled thread

[OSDev] How to switch to long mode in x86 CPUs?

Started byAr Rakin <rakinar2@onesoftnet.eu.org>
First post2025-02-27 21:57 +0600
Last post2025-03-01 20:02 -0800
Articles 20 on this page of 31 — 13 participants

Back to article view | Back to comp.lang.c


Contents

  [OSDev] How to switch to long mode in x86 CPUs? Ar Rakin <rakinar2@onesoftnet.eu.org> - 2025-02-27 21:57 +0600
    Re: [OSDev] How to switch to long mode in x86 CPUs? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-27 14:02 -0800
      Re: [OSDev] How to switch to long mode in x86 CPUs? Ar Rakin <rakinar2@onesoftnet.eu.org> - 2025-02-28 22:18 +0600
        Re: [OSDev] How to switch to long mode in x86 CPUs? antispam@fricas.org (Waldek Hebisch) - 2025-02-28 18:25 +0000
        OT: (Was: How to switch to long mode in x86 CPUs?) Salvador Mirzo <smirzo@example.com> - 2025-02-28 19:56 -0300
          Re: OT: USENET Salvador Mirzo <smirzo@example.com> - 2025-02-28 19:57 -0300
            Re: OT: USENET Richard Harnden <richard.nospam@gmail.invalid> - 2025-02-28 23:38 +0000
              Re: OT: USENET Ar Rakin <rakinar2@onesoftnet.eu.org> - 2025-03-01 15:09 +0600
                Re: OT: USENET candycanearter07 <candycanearter07@candycanearter07.nomail.afraid> - 2025-03-05 19:30 +0000
              Re: OT: USENET Salvador Mirzo <smirzo@example.com> - 2025-03-01 23:34 -0300
                Re: OT: USENET Ar Rakin <rakinar2@onesoftnet.eu.org> - 2025-03-02 16:48 +0600
                  Re: OT: USENET yeti <yeti@tilde.institute> - 2025-03-02 17:27 +0042
                  Re: OT: USENET Salvador Mirzo <smirzo@example.com> - 2025-03-08 20:06 -0300
            Re: OT: USENET Ar Rakin <rakinar2@onesoftnet.eu.org> - 2025-03-01 15:08 +0600
          Re: OT: Ar Rakin <rakinar2@onesoftnet.eu.org> - 2025-03-01 15:07 +0600
    Re: [OSDev] How to switch to long mode in x86 CPUs? David Brown <david.brown@hesbynett.no> - 2025-02-28 09:59 +0100
      Re: [OSDev] How to switch to long mode in x86 CPUs? Ar Rakin <rakinar2@onesoftnet.eu.org> - 2025-02-28 22:30 +0600
        Re: [OSDev] How to switch to long mode in x86 CPUs? cross@spitfire.i.gajendra.net (Dan Cross) - 2025-03-01 13:34 +0000
      Re: [OSDev] How to switch to long mode in x86 CPUs? "Paul Edwards" <mutazilah@gmail.com> - 2025-03-01 15:50 +1100
        Re: [OSDev] How to switch to long mode in x86 CPUs? scott@slp53.sl.home (Scott Lurndal) - 2025-03-01 14:55 +0000
          Re: [OSDev] How to switch to long mode in x86 CPUs? Salvador Mirzo <smirzo@example.com> - 2025-03-01 23:37 -0300
            Re: PC BIOS (was [OSDev] How to switch to long mode in x86 CPUs?) cross@spitfire.i.gajendra.net (Dan Cross) - 2025-03-02 16:01 +0000
            Re: [OSDev] How to switch to long mode in x86 CPUs? scott@slp53.sl.home (Scott Lurndal) - 2025-03-02 16:42 +0000
              Re: [OSDev] How to switch to long mode in x86 CPUs? cross@spitfire.i.gajendra.net (Dan Cross) - 2025-03-02 20:26 +0000
          Re: [OSDev] How to switch to long mode in x86 CPUs? "Paul Edwards" <mutazilah@gmail.com> - 2025-03-02 19:22 +1100
            Re: [OSDev] How to switch to long mode in x86 CPUs? bart <bc@freeuk.com> - 2025-03-02 16:16 +0000
              Re: [OSDev] How to switch to long mode in x86 CPUs? "Paul Edwards" <mutazilah@gmail.com> - 2025-03-05 05:31 +1100
                Re: [OSDev] How to switch to long mode in x86 CPUs? bart <bc@freeuk.com> - 2025-03-04 20:27 +0000
                  Re: [OSDev] How to switch to long mode in x86 CPUs? "Paul Edwards" <mutazilah@gmail.com> - 2025-03-05 16:38 +1100
    Re: [OSDev] How to switch to long mode in x86 CPUs? cross@spitfire.i.gajendra.net (Dan Cross) - 2025-03-01 13:15 +0000
    Re: [OSDev] How to switch to long mode in x86 CPUs? Andy Valencia <vandys@vsta.org> - 2025-03-01 20:02 -0800

Page 1 of 2  [1] 2  Next page →


#390518 — [OSDev] How to switch to long mode in x86 CPUs?

FromAr Rakin <rakinar2@onesoftnet.eu.org>
Date2025-02-27 21:57 +0600
Subject[OSDev] How to switch to long mode in x86 CPUs?
Message-ID<871pvje5yq.fsf@onesoftnet.eu.org>
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!

--
Ar Rakin

[toc] | [next] | [standalone]


#390533

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2025-02-27 14:02 -0800
Message-ID<87y0xrnizx.fsf@nosuchdomain.example.com>
In reply to#390518
Ar Rakin <rakinar2@onesoftnet.eu.org> writes:
> 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:
>
[67 lines deleted]
>
> 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.

(Everyone else: Let's not start a debate about the merits of Stack
Overflow.)

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [next] | [standalone]


#390569

FromAr Rakin <rakinar2@onesoftnet.eu.org>
Date2025-02-28 22:18 +0600
Message-ID<87a5a6gi13.fsf@onesoftnet.eu.org>
In reply to#390533
On 27 Feb 2025 14:02, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:

> Ar Rakin <rakinar2@onesoftnet.eu.org> writes:
>> 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:
>>
> [67 lines deleted]
>>
>> 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.)

There are no other active Usenet groups unfortunately.  At least I don't
know any.

> You might consider posting on Stack Overflow.

That's no fun :/

I discovered Usenet a week ago, started using it, and I'm loving it.  You
can say that I am one of those people who love retro-computing.  Usenet
is much more fun to use than web forums or things like that.

--
Ar Rakin

[toc] | [prev] | [next] | [standalone]


#390572

Fromantispam@fricas.org (Waldek Hebisch)
Date2025-02-28 18:25 +0000
Message-ID<vpsv2p$brsl$2@paganini.bofh.team>
In reply to#390569
Ar Rakin <rakinar2@onesoftnet.eu.org> wrote:
> 
> There are no other active Usenet groups unfortunately.  At least I don't
> know any.

There alt.os.development and (IIRC) alt.os.assembly which are quite
good fit for your question.  They have little trafic now, but there
are people reading them and answering.

-- 
                              Waldek Hebisch

[toc] | [prev] | [next] | [standalone]


#390585 — OT: (Was: How to switch to long mode in x86 CPUs?)

FromSalvador Mirzo <smirzo@example.com>
Date2025-02-28 19:56 -0300
SubjectOT: (Was: How to switch to long mode in x86 CPUs?)
Message-ID<87jz99bruv.fsf_-_@example.com>
In reply to#390569
Ar Rakin <rakinar2@onesoftnet.eu.org> writes:

[...]

>> You might consider posting on Stack Overflow.
>
> That's no fun :/

Might also enjoy it while you can.  With the advent of text generators,
the future of this website might be quite uncertain.

> I discovered Usenet a week ago, started using it, and I'm loving it.  You
> can say that I am one of those people who love retro-computing.  Usenet
> is much more fun to use than web forums or things like that.

Quite right.  So much for fun being here.

[toc] | [prev] | [next] | [standalone]


#390586 — Re: OT: USENET

FromSalvador Mirzo <smirzo@example.com>
Date2025-02-28 19:57 -0300
SubjectRe: OT: USENET
Message-ID<87cyf1brtc.fsf_-_@example.com>
In reply to#390585
Sorry---I messed up the subject.  Fixed it.

Salvador Mirzo <smirzo@example.com> writes:

> Ar Rakin <rakinar2@onesoftnet.eu.org> writes:
>
> [...]
>
>>> You might consider posting on Stack Overflow.
>>
>> That's no fun :/
>
> Might also enjoy it while you can.  With the advent of text generators,
> the future of this website might be quite uncertain.
>
>> I discovered Usenet a week ago, started using it, and I'm loving it.  You
>> can say that I am one of those people who love retro-computing.  Usenet
>> is much more fun to use than web forums or things like that.
>
> Quite right.  So much for fun being here.

[toc] | [prev] | [next] | [standalone]


#390589 — Re: OT: USENET

FromRichard Harnden <richard.nospam@gmail.invalid>
Date2025-02-28 23:38 +0000
SubjectRe: OT: USENET
Message-ID<vpthdk$3sm80$2@dont-email.me>
In reply to#390586
On 28/02/2025 22:57, Salvador Mirzo wrote:
> Sorry---I messed up the subject.  Fixed it.
> 
> Salvador Mirzo <smirzo@example.com> writes:
> 
>> Ar Rakin <rakinar2@onesoftnet.eu.org> writes:
>>
>> [...]
>>
>>>> You might consider posting on Stack Overflow.
>>>
>>> That's no fun :/
>>
>> Might also enjoy it while you can.  With the advent of text generators,
>> the future of this website might be quite uncertain.

Usenet it not a website.

Probably the only thing LLMs don't scrape.

>>
>>> I discovered Usenet a week ago, started using it, and I'm loving it.  You
>>> can say that I am one of those people who love retro-computing.  Usenet
>>> is much more fun to use than web forums or things like that.
>>
>> Quite right.  So much for fun being here.

[toc] | [prev] | [next] | [standalone]


#390600 — Re: OT: USENET

FromAr Rakin <rakinar2@onesoftnet.eu.org>
Date2025-03-01 15:09 +0600
SubjectRe: OT: USENET
Message-ID<87v7struar.fsf@onesoftnet.eu.org>
In reply to#390589
On 28 Feb 2025 23:38, Richard Harnden <richard.nospam@gmail.invalid> wrote:

> Usenet it not a website.
>
> Probably the only thing LLMs don't scrape.

I'd want it to be like this forever.  Don't let the AI companies know
about Usenet!

--
Ar Rakin

[toc] | [prev] | [next] | [standalone]


#390817 — Re: OT: USENET

Fromcandycanearter07 <candycanearter07@candycanearter07.nomail.afraid>
Date2025-03-05 19:30 +0000
SubjectRe: OT: USENET
Message-ID<slrnvsh91i.2chr6.candycanearter07@candydeb.host.invalid>
In reply to#390600
Ar Rakin <rakinar2@onesoftnet.eu.org> wrote at 09:09 this Saturday (GMT):
> On 28 Feb 2025 23:38, Richard Harnden <richard.nospam@gmail.invalid> wrote:
>
>> Usenet it not a website.
>>
>> Probably the only thing LLMs don't scrape.
>
> I'd want it to be like this forever.  Don't let the AI companies know
> about Usenet!
>
> --
> Ar Rakin


I mean, Google cut us off so..
-- 
user <candycane> is generated from /dev/urandom

[toc] | [prev] | [next] | [standalone]


#390619 — Re: OT: USENET

FromSalvador Mirzo <smirzo@example.com>
Date2025-03-01 23:34 -0300
SubjectRe: OT: USENET
Message-ID<871pvgjh3j.fsf@example.com>
In reply to#390589
Richard Harnden <richard.nospam@gmail.invalid> writes:

> On 28/02/2025 22:57, Salvador Mirzo wrote:
>> Sorry---I messed up the subject.  Fixed it.
>> Salvador Mirzo <smirzo@example.com> writes:
>> 
>>> Ar Rakin <rakinar2@onesoftnet.eu.org> writes:
>>>
>>> [...]
>>>
>>>>> You might consider posting on Stack Overflow.
>>>>
>>>> That's no fun :/
>>>
>>> Might also enjoy it while you can.  With the advent of text generators,
>>> the future of this website might be quite uncertain.
>
> Usenet it not a website.

LOL---I meant Stack Overflow.  I would think they're very much down by
now in page views because I'd think it's used most by beginner
programmers who would typically try text generators before they go to
Stack Overflow and will usually be satisfied.  So I think Stack
Overflow's future is quite uncertain.

And, by the way, I didn't think it would happen so fast, but since it
started out, I thought---it can't go too far because they don't quite
let programmers talk to one another, like we do here.  A
question-and-answer website is poor.  It's useful, but it's poor.

Discoveries come about not by asking and answering, but by writing, so
we need to write to learn and understand.  Of course, the writing should
be of high-quality, which is achievable if the community enforces it.

[toc] | [prev] | [next] | [standalone]


#390634 — Re: OT: USENET

FromAr Rakin <rakinar2@onesoftnet.eu.org>
Date2025-03-02 16:48 +0600
SubjectRe: OT: USENET
Message-ID<vq1d2b$o5ud$1@dont-email.me>
In reply to#390619
On 3/2/25 8:34 AM, Salvador Mirzo wrote:
> Richard Harnden <richard.nospam@gmail.invalid> writes:
> 
>> On 28/02/2025 22:57, Salvador Mirzo wrote:
>>> Sorry---I messed up the subject.  Fixed it.
>>> Salvador Mirzo <smirzo@example.com> writes:
>>>
>>>> Ar Rakin <rakinar2@onesoftnet.eu.org> writes:
>>>>
>>>> [...]
>>>>
>>>>>> You might consider posting on Stack Overflow.
>>>>>
>>>>> That's no fun :/
>>>>
>>>> Might also enjoy it while you can.  With the advent of text generators,
>>>> the future of this website might be quite uncertain.
>>
>> Usenet it not a website.
> 
> LOL---I meant Stack Overflow.  I would think they're very much down by
> now in page views because I'd think it's used most by beginner
> programmers who would typically try text generators before they go to
> Stack Overflow and will usually be satisfied.  So I think Stack
> Overflow's future is quite uncertain.
> 
> And, by the way, I didn't think it would happen so fast, but since it
> started out, I thought---it can't go too far because they don't quite
> let programmers talk to one another, like we do here.  A
> question-and-answer website is poor.  It's useful, but it's poor.
> 
> Discoveries come about not by asking and answering, but by writing, so
> we need to write to learn and understand.  Of course, the writing should
> be of high-quality, which is achievable if the community enforces it.

The StackOverflow community is extremely toxic to newbies - instead of 
asking and answering questions like a healthy and friendly community. 
Refer to this YouTube video for more details on this: 
<https://www.youtube.com/watch?v=N7v0yvdkIHg>.

I stopped using StackOverflow years ago.

-- 
Rakin

[toc] | [prev] | [next] | [standalone]


#390691 — Re: OT: USENET

Fromyeti <yeti@tilde.institute>
Date2025-03-02 17:27 +0042
SubjectRe: OT: USENET
Message-ID<87ikoq19xn.fsf@tilde.institute>
In reply to#390634
Ar Rakin <rakinar2@onesoftnet.eu.org> wrote:

> I stopped using StackOverflow years ago.

Same.

I kept the impression counting up-votes and weaponizing down-votes was
the main game that was played there.

-- 
... and do not forget to unsubscribe.

[toc] | [prev] | [next] | [standalone]


#390909 — Re: OT: USENET

FromSalvador Mirzo <smirzo@example.com>
Date2025-03-08 20:06 -0300
SubjectRe: OT: USENET
Message-ID<87y0xf9l6e.fsf@example.com>
In reply to#390634
Ar Rakin <rakinar2@onesoftnet.eu.org> writes:

> On 3/2/25 8:34 AM, Salvador Mirzo wrote:

[...]

> The StackOverflow community is extremely toxic to newbies - instead of
> asking and answering questions like a healthy and friendly
> community. Refer to this YouTube video for more details on this:
> <https://www.youtube.com/watch?v=N7v0yvdkIHg>.
>
> I stopped using StackOverflow years ago.

I never began.

[toc] | [prev] | [next] | [standalone]


#390599 — Re: OT: USENET

FromAr Rakin <rakinar2@onesoftnet.eu.org>
Date2025-03-01 15:08 +0600
SubjectRe: OT: USENET
Message-ID<87zfi5rudm.fsf@onesoftnet.eu.org>
In reply to#390586
On 28 Feb 2025 19:57, Salvador Mirzo <smirzo@example.com> wrote:

> Sorry---I messed up the subject.  Fixed it.
>
> Salvador Mirzo <smirzo@example.com> writes:
>
>> Ar Rakin <rakinar2@onesoftnet.eu.org> writes:
>>
>> [...]
>>
>>>> You might consider posting on Stack Overflow.
>>>
>>> That's no fun :/
>>
>> Might also enjoy it while you can.  With the advent of text generators,
>> the future of this website might be quite uncertain.
>>
>>> I discovered Usenet a week ago, started using it, and I'm loving it.  You
>>> can say that I am one of those people who love retro-computing.  Usenet
>>> is much more fun to use than web forums or things like that.
>>
>> Quite right.  So much for fun being here.

Happens! It's okay.

--
Ar Rakin

[toc] | [prev] | [next] | [standalone]


#390598 — Re: OT:

FromAr Rakin <rakinar2@onesoftnet.eu.org>
Date2025-03-01 15:07 +0600
SubjectRe: OT:
Message-ID<877c59t8z6.fsf@onesoftnet.eu.org>
In reply to#390585
On 28 Feb 2025 19:56, Salvador Mirzo <smirzo@example.com> wrote:

> Ar Rakin <rakinar2@onesoftnet.eu.org> writes:
>
> [...]
>
>>> You might consider posting on Stack Overflow.
>>
>> That's no fun :/
>
> Might also enjoy it while you can.  With the advent of text generators,
> the future of this website might be quite uncertain.
>
>> I discovered Usenet a week ago, started using it, and I'm loving it.  You
>> can say that I am one of those people who love retro-computing.  Usenet
>> is much more fun to use than web forums or things like that.
>
> Quite right.  So much for fun being here.

Yes!

--
Ar Rakin

[toc] | [prev] | [next] | [standalone]


#390543

FromDavid Brown <david.brown@hesbynett.no>
Date2025-02-28 09:59 +0100
Message-ID<vprtt6$3jah9$1@dont-email.me>
In reply to#390518
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.

[toc] | [prev] | [next] | [standalone]


#390570

FromAr Rakin <rakinar2@onesoftnet.eu.org>
Date2025-02-28 22:30 +0600
Message-ID<875xkughgw.fsf@onesoftnet.eu.org>
In reply to#390543
On 28 Feb 2025 09:59, David Brown <david.brown@hesbynett.no> wrote:

> 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.

I am just trying to learn about OS kernels, by building one.

> 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.

I actually wanted to build a simple kernel that would run specifically
on x86_64 systems.  I understand why you are recommending me to learn
about microcontrollers first, but at the moment I am almost done with
what I wanted to achieve - I just need to fix the paging issue I
described earlier.

> 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.

> 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.

That is true, I could have just used C.  Even articles on the osdev.org
website heavily use C, for stuff like setting up the Global Descriptor
Table (GDT), which I prefer to do in assembly because you would still
need to use a special x86 assembly instruction to load the GDT (lgdt).

I would much rather prefer having separate assembly files rather than
inlining them in the C source files whenever possible.  It's probably
just my preference.

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.

--
Ar Rakin

[toc] | [prev] | [next] | [standalone]


#390602

Fromcross@spitfire.i.gajendra.net (Dan Cross)
Date2025-03-01 13:34 +0000
Message-ID<vpv2di$5t5$1@reader1.panix.com>
In reply to#390570
[Note: Followup-To: alt.os.development]

In article <875xkughgw.fsf@onesoftnet.eu.org>,
Ar Rakin  <rakinar2@onesoftnet.eu.org> wrote:
>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.

Comp.arch is not a good choice here.  It's for discussion of
computer architecutre generally, specifics of how to boot an x86
core would be mostly tangential.

alt.os.development is probably your best bet, but please try not
to feed the troll there.

>[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.

Eh...It can be done, but I don't see the point.  Even starting
from the reset vector (e.g., in 16-bit 8086 mode) it's not that
many instructions to get yourself into long mode with paging
enabled.  To see an example of how this is done, have a look at:
https://github.com/oxidecomputer/phbl/blob/main/src/start.S

It's sufficient straight-forward, and so low-level, that doing
this in a high-level language doesn't seem worth it from a
cost/benefit perspective.

	- Dan C.

[toc] | [prev] | [next] | [standalone]


#390595

From"Paul Edwards" <mutazilah@gmail.com>
Date2025-03-01 15:50 +1100
Message-ID<vpu3m5$3804$1@dont-email.me>
In reply to#390543
"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:

> > 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'm not disputing anything you have said, I'm just offering a variation.

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?

If so, then with a suitable BIOS (or pseudo-bios if you want to
quibble what a BIOS is), you can have an operating system
(memory management, file management, program loading,
and an API) written in pure C90, except for the implementation
of setjmp/longjmp which is necessarily assembler (or non-standard
C).

That's what PDOS-generic (at pdos.org) is.

> I
> remember reading once about LinuxBIOS (now coreboot) having just four
> lines of assembly running out of reset, before everything else was in C.

And that sounds like a BIOS - it's in the original name, not
the OS proper.

BFN. Paul.

[toc] | [prev] | [next] | [standalone]


#390603

Fromscott@slp53.sl.home (Scott Lurndal)
Date2025-03-01 14:55 +0000
Message-ID<JdFwP.46247$SZca.36276@fx13.iad>
In reply to#390595
"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.

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.c


csiph-web