Changeset 811770c in mainline for kernel/arch/amd64/src


Ignore:
Timestamp:
2016-05-05T12:06:04Z (10 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
57c2a87
Parents:
0f17bff
Message:

Avoid magic numbers and specialized functions to set/get register bits

Location:
kernel/arch/amd64/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • kernel/arch/amd64/src/amd64.c

    r0f17bff r811770c  
    6464#endif
    6565
    66 /** Disable I/O on non-privileged levels
    67  *
    68  * Clean IOPL(12,13) and NT(14) flags in EFLAGS register
    69  */
    70 static void clean_IOPL_NT_flags(void)
    71 {
    72         asm volatile (
    73                 "pushfq\n"
    74                 "pop %%rax\n"
    75                 "and $~(0x7000), %%rax\n"
    76                 "pushq %%rax\n"
    77                 "popfq\n"
    78                 ::: "%rax"
    79         );
    80 }
    81 
    82 /** Disable alignment check
    83  *
    84  * Clean AM(18) flag in CR0 register
    85  */
    86 static void clean_AM_flag(void)
    87 {
    88         asm volatile (
    89                 "mov %%cr0, %%rax\n"
    90                 "and $~(0x40000), %%rax\n"
    91                 "mov %%rax, %%cr0\n"
    92                 ::: "%rax"
    93         );
    94 }
    95 
    9666/** Perform amd64-specific initialization before main_bsp() is called.
    9767 *
     
    11686{
    11787        /* Enable no-execute pages */
    118         set_efer_flag(AMD_NXE_FLAG);
     88        write_msr(AMD_MSR_EFER, read_msr(AMD_MSR_EFER) | AMD_NXE);
    11989        /* Enable FPU */
    12090        cpu_setup_fpu();
     
    12393        pm_init();
    12494       
    125         /* Disable I/O on nonprivileged levels
    126          * clear the NT (nested-thread) flag
    127          */
    128         clean_IOPL_NT_flags();
     95        /* Disable I/O on nonprivileged levels, clear the nested-thread flag */
     96        write_rflags(read_rflags() & ~(RFLAGS_IOPL | RFLAGS_NT));
    12997        /* Disable alignment check */
    130         clean_AM_flag();
     98        write_cr0(read_cr0() & ~CR0_AM);
    13199       
    132100        if (config.cpu_active == 1) {
  • kernel/arch/amd64/src/asm.S

    r0f17bff r811770c  
    3333#include <arch/kseg_struct.h>
    3434#include <arch/cpu.h>
     35#include <arch/smp/apic.h>
    3536
    3637.text
     
    9394       
    9495        /* Flip the ID bit */
    95         btcl $21, %edx
     96        xorl $RFLAGS_ID, %edx
    9697       
    9798        /* Store RFLAGS */
     
    102103        /* Get the ID bit again */
    103104        popq %rdx
    104         andl $(1 << 21), %eax
    105         andl $(1 << 21), %edx
     105        andl $RFLAGS_ID, %eax
     106        andl $RFLAGS_ID, %edx
    106107       
    107108        /* 0 if not supported, 1 if supported */
     
    127128FUNCTION_END(cpuid)
    128129
    129 FUNCTION_BEGIN(set_efer_flag)
    130         movl $0xc0000080, %ecx
     130/** Enable local APIC
     131 *
     132 * Enable local APIC in MSR.
     133 *
     134 */
     135FUNCTION_BEGIN(enable_l_apic_in_msr)
     136        movl $AMD_MSR_APIC_BASE, %ecx
    131137        rdmsr
    132         btsl %edi, %eax
     138        orl $(L_APIC_BASE | AMD_APIC_BASE_GE), %eax
    133139        wrmsr
    134140        ret
    135 FUNCTION_END(set_efer_flag)
    136 
    137 FUNCTION_BEGIN(read_efer_flag)
    138         movl $0xc0000080, %ecx
    139         rdmsr
    140         ret
    141 FUNCTION_END(read_efer_flag)
     141FUNCTION_END(enable_l_apic_in_msr)
    142142
    143143/*
     
    541541        ret
    542542FUNCTION_END(early_putchar)
    543 
  • kernel/arch/amd64/src/boot/multiboot.S

    r0f17bff r811770c  
    168168       
    169169        movl %cr4, %eax
    170         btsl $5, %eax
     170        orl $CR4_PAE, %eax
    171171        movl %eax, %cr4
    172172       
     
    176176       
    177177        /* Enable long mode */
    178         movl $EFER_MSR_NUM, %ecx
     178        movl $AMD_MSR_EFER, %ecx
    179179        rdmsr                     /* read EFER */
    180         btsl $AMD_LME_FLAG, %eax  /* set LME = 1 */
     180        orl $AMD_LME, %eax        /* set LME = 1 */
    181181        wrmsr
    182182       
    183183        /* Enable paging to activate long mode (set CR0.PG = 1) */
    184184        movl %cr0, %eax
    185         btsl $31, %eax
     185        orl $CR0_PG, %eax
    186186        movl %eax, %cr0
    187187       
  • kernel/arch/amd64/src/boot/multiboot2.S

    r0f17bff r811770c  
    209209       
    210210        movl %cr4, %eax
    211         btsl $5, %eax
     211        orl $CR4_PAE, %eax
    212212        movl %eax, %cr4
    213213       
     
    217217       
    218218        /* Enable long mode */
    219         movl $EFER_MSR_NUM, %ecx
     219        movl $AMD_MSR_EFER, %ecx
    220220        rdmsr                     /* read EFER */
    221         btsl $AMD_LME_FLAG, %eax  /* set LME = 1 */
     221        orl $AMD_LME, %eax        /* set LME = 1 */
    222222        wrmsr
    223223       
    224224        /* Enable paging to activate long mode (set CR0.PG = 1) */
    225225        movl %cr0, %eax
    226         btsl $31, %eax
     226        orl $CR0_PG, %eax
    227227        movl %eax, %cr0
    228228       
  • kernel/arch/amd64/src/cpu/cpu.c

    r0f17bff r811770c  
    7676void cpu_setup_fpu(void)
    7777{
    78         asm volatile (
    79                 "movq %%cr0, %%rax\n"
    80                 "btsq $1, %%rax\n"  /* cr0.mp */
    81                 "btrq $2, %%rax\n"  /* cr0.em */
    82                 "movq %%rax, %%cr0\n"
    83                
    84                 "movq %%cr4, %%rax\n"
    85                 "bts $9, %%rax\n"   /* cr4.osfxsr */
    86                 "movq %%rax, %%cr4\n"
    87                 ::: "%rax"
    88         );
     78        write_cr0((read_cr0() & ~CR0_EM) | CR0_MP);
     79        write_cr4(read_cr4() | CR4_OSFXSR);
    8980}
    9081
     
    9788void fpu_disable(void)
    9889{
    99         asm volatile (
    100                 "mov %%cr0, %%rax\n"
    101                 "bts $3, %%rax\n"
    102                 "mov %%rax, %%cr0\n"
    103                 ::: "%rax"
    104         );
     90        write_cr0(read_cr0() | CR0_TS);
    10591}
    10692
    10793void fpu_enable(void)
    10894{
    109         asm volatile (
    110                 "mov %%cr0, %%rax\n"
    111                 "btr $3, %%rax\n"
    112                 "mov %%rax, %%cr0\n"
    113                 ::: "%rax"
    114         );
     95        write_cr0(read_cr0() & ~CR0_TS);
    11596}
    11697
  • kernel/arch/amd64/src/smp/ap.S

    r0f17bff r811770c  
    7575       
    7676        movl %cr4, %eax
    77         btsl $5, %eax
     77        orl $CR4_PAE, %eax
    7878        movl %eax, %cr4
    7979       
     
    8282       
    8383        # Enable long mode
    84         movl $EFER_MSR_NUM, %ecx  # EFER MSR number
     84        movl $AMD_MSR_EFER, %ecx  # EFER MSR number
    8585        rdmsr                     # Read EFER
    86         btsl $AMD_LME_FLAG, %eax  # Set LME=1
     86        orl $AMD_LME, %eax        # Set LME=1
    8787        wrmsr                     # Write EFER
    8888       
    8989        # Enable paging to activate long mode (set CR0.PG = 1)
    9090        movl %cr0, %eax
    91         btsl $31, %eax
     91        orl $CR0_PG, %eax
    9292        movl %eax, %cr0
    9393       
  • kernel/arch/amd64/src/syscall.c

    r0f17bff r811770c  
    4848{
    4949        /* Enable SYSCALL/SYSRET */
    50         set_efer_flag(AMD_SCE_FLAG);
     50        write_msr(AMD_MSR_EFER, read_msr(AMD_MSR_EFER) | AMD_SCE);
    5151
    5252        /* Setup syscall entry address */
  • kernel/arch/amd64/src/userspace.c

    r0f17bff r811770c  
    4848void userspace(uspace_arg_t *kernel_uarg)
    4949{
    50         ipl_t ipl = interrupts_disable();
     50        uint64_t rflags = read_rflags();
    5151       
    52         ipl &= ~(RFLAGS_CF | RFLAGS_PF | RFLAGS_AF | RFLAGS_ZF | RFLAGS_SF |
    53             RFLAGS_DF | RFLAGS_OF);
     52        rflags &= ~RFLAGS_NT;
     53        rflags |= RFLAGS_IF;
    5454       
    5555        asm volatile (
    5656                "pushq %[udata_des]\n"
    5757                "pushq %[stack_top]\n"
    58                 "pushq %[ipl]\n"
     58                "pushq %[rflags]\n"
    5959                "pushq %[utext_des]\n"
    6060                "pushq %[entry]\n"
     
    6767                   [stack_top] "r" ((uint8_t *) kernel_uarg->uspace_stack +
    6868                       kernel_uarg->uspace_stack_size),
    69                    [ipl] "r" (ipl),
     69                   [rflags] "r" (rflags),
    7070                   [utext_des] "i" (GDT_SELECTOR(UTEXT_DES) | PL_USER),
    7171                   [entry] "r" (kernel_uarg->uspace_entry),
Note: See TracChangeset for help on using the changeset viewer.