{"id":5503,"date":"2014-03-30T23:15:46","date_gmt":"2014-03-30T23:15:46","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/why-does-switching-to-protected-restarts-the-machine-collection-of-common-programming-errors\/"},"modified":"2014-03-30T23:15:46","modified_gmt":"2014-03-30T23:15:46","slug":"why-does-switching-to-protected-restarts-the-machine-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/why-does-switching-to-protected-restarts-the-machine-collection-of-common-programming-errors\/","title":{"rendered":"Why does switching to protected restarts the machine?-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to create very simple operating system in 64 bits. I&#8217;m trying to enter protected mode first, but I&#8217;m failing at this point.<\/p>\n<p>When I do the far jump into 32 bits, the machine restarts.<\/p>\n<p>My code is loaded into memory with another assembly program at the address 0x100.<\/p>\n<p>The code is compiled with nasm and I&#8217;m running the program using <code>qemu -fda<\/code>.<\/p>\n<p>Here is the code I got so far:<\/p>\n<pre><code>[BITS 16]\n\njmp _start\n\n_start:\n    cli\n\n    lgdt [GDT64]\n\n    ; Switch to protected mode\n    mov eax, cr0\n    or al, 1b\n    mov cr0, eax\n\n    ; Desactivate pagination\n    mov eax, cr0\n    and eax, 01111111111111111111111111111111b\n    mov cr0, eax\n\n    jmp (CODE_SELECTOR-GDT64):pm_start\n\n[BITS 32]\n\npm_start:\n    jmp $\n\nGDT64:\n    NULL_SELECTOR:\n        dw GDT_LENGTH   ; limit of GDT\n        dw GDT64        ; linear address of GDT\n        dd 0x0\n\n    CODE_SELECTOR:          ; 32-bit code selector (ring 0)\n        dw 0x0FFFF\n        db 0x0, 0x0, 0x0\n        db 10011010b\n        db 11001111b\n        db 0x0\n\n    DATA_SELECTOR:          ; flat data selector (ring 0)\n        dw  0x0FFFF\n        db  0x0, 0x0, 0x0\n        db  10010010b\n        db  10001111b\n        db  0x0\n\n    LONG_SELECTOR:  ; 64-bit code selector (ring 0)\n        dw  0x0FFFF\n        db  0x0, 0x0, 0x0\n        db  10011010b       ;\n        db  10101111b\n        db  0x0\n\n   GDT_LENGTH:\n<\/code><\/pre>\n<p>If I do a <code>jmp $<\/code> before the long jump, it works, the program is halted correctly, but when the long jump is done, it reboots the machine.<\/p>\n<p>Did I forgot to setup something like a segment or something like that?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to create very simple operating system in 64 bits. I&#8217;m trying to enter protected mode first, but I&#8217;m failing at this point. When I do the far jump into 32 bits, the machine restarts. My code is loaded into memory with another assembly program at the address 0x100. The code is compiled with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5503","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5503"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5503\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}