CRx registers and paging in linux - x86

0

I'm trying to learn a bit about the linux kernel and memory management. To do this I've written a small bit of kernel module code to dump CR0 register content. I understand that bit 31 in CR0, when set, indicates that paging has been turned on by the kernel however, when I print CR0 I see (for my particular instance) that it is set to 0x2. This suggests (if I understand correctly) that both paging is disabled and that the processor is in real mode (bit 0 is also unset). This surprised me as I expected protected mode/paging memory - don't all multi-tasking OS's do this? Can the kernel run in real mode/non-paging and user space be protected mode/paging? Can someone explain why I might see what I'm seeing here?

Note that I'm running the 2.6.18-274.el5 kernel (64-bit RHEL 5 binary).

I had a small bug in my code so that I was printing random garbage instead of CR0 register. Below is the working code - note that you don't need to call the read_c0 function if you are coding in assembly...

.globl init_module
.globl cleanup_module
.text
init_module:
    nop
    movq    $ENTER_MSG,     %rdi
    movq    %cr3,           %rsi
    movq    %rsi,           %rdx
    shrq    $12,            %rdx
    movq    %cr0,           %r11
    movq    $FALSE,         %rcx
    andq    PAGING_BIT_31,  %r11
    cmpq    PAGING_BIT_31,  %r11
    jne     .CONT1
    movq    $TRUE,          %rcx
.CONT1:
    movq    $FALSE,         %r8
    movq    %cr0,           %r12
    andq    PROTMOD_BIT_0,  %r12
    cmpq    PROTMOD_BIT_0,  %r12
    jne     .CONT2
    movq    $TRUE,          %r8
.CONT2:
    movq    %cr0,           %r9
    xorq    %rax,           %rax
    callq   printk
    xorq    %rax,           %rax
    retq

cleanup_module:
    nop
    movq    $LEAVE_MSG,     %rdi
    movq    %cr3,           %rsi
    xorq    %rax,           %rax
    callq   printk
    retq

.section .rodata
ENTER_MSG:
    .asciz "\n\nHELLO! CR3: %p, pCR3: %p \n\tPAGING IS %s\n\tPROTECTED MODE IS %s\n\tCR0: %p\n" 
LEAVE_MSG:
    .asciz "GOODBYE! CR3: %p\n"
PAGING_BIT_31:
    .quad 0x80000000
PROTMOD_BIT_0:
    .quad 0x1
FALSE:
    .asciz "OFF"
TRUE:
    .asciz "ON"
linux
memory-management
assembly
linux-kernel
kernel
asked on Stack Overflow Jul 26, 2013 by boneheadgeek • edited Jul 29, 2013 by boneheadgeek

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0