Changeset f24d300 in mainline


Ignore:
Timestamp:
2009-03-03T15:52:55Z (15 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e762b43
Parents:
add04f7
Message:

better inline assembler readability using the new symbolic syntax

Location:
kernel/arch/amd64
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • kernel/arch/amd64/include/asm.h

    radd04f7 rf24d300  
    2727 */
    2828
    29 /** @addtogroup amd64   
     29/** @addtogroup amd64
    3030 * @{
    3131 */
     
    4646 * The stack is assumed to be STACK_SIZE bytes long.
    4747 * The stack must start on page boundary.
     48 *
    4849 */
    4950static inline uintptr_t get_stack_base(void)
     
    5152        uintptr_t v;
    5253       
    53         asm volatile ("andq %%rsp, %0\n" : "=r" (v) : "0" (~((uint64_t)STACK_SIZE-1)));
     54        asm volatile (
     55                "andq %%rsp, %[v]\n"
     56                : [v] "=r" (v)
     57                : "0" (~((uint64_t) STACK_SIZE-1))
     58        );
    5459       
    5560        return v;
     
    7378 * @param port Port to read from
    7479 * @return Value read
     80 *
    7581 */
    7682static inline uint8_t pio_read_8(ioport8_t *port)
    7783{
    7884        uint8_t val;
    79 
    80         asm volatile ("inb %w1, %b0 \n" : "=a" (val) : "d" (port));
     85       
     86        asm volatile (
     87                "inb %w[port], %b[val]\n"
     88                : [val] "=a" (val)
     89                : [port] "d" (port)
     90        );
     91       
    8192        return val;
    8293}
     
    8899 * @param port Port to read from
    89100 * @return Value read
     101 *
    90102 */
    91103static inline uint16_t pio_read_16(ioport16_t *port)
     
    93105        uint16_t val;
    94106       
    95         asm volatile ("inw %w1, %w0 \n" : "=a" (val) : "d" (port));
     107        asm volatile (
     108                "inw %w[port], %w[val]\n"
     109                : [val] "=a" (val)
     110                : [port] "d" (port)
     111        );
     112       
    96113        return val;
    97114}
     
    103120 * @param port Port to read from
    104121 * @return Value read
     122 *
    105123 */
    106124static inline uint32_t pio_read_32(ioport32_t *port)
     
    108126        uint32_t val;
    109127       
    110         asm volatile ("inl %w1, %0 \n" : "=a" (val) : "d" (port));
     128        asm volatile (
     129                "inl %w[port], %[val]\n"
     130                : [val] "=a" (val)
     131                : [port] "d" (port)
     132        );
     133       
    111134        return val;
    112135}
     
    118141 * @param port Port to write to
    119142 * @param val Value to write
     143 *
    120144 */
    121145static inline void pio_write_8(ioport8_t *port, uint8_t val)
    122146{
    123         asm volatile ("outb %b0, %w1\n" : : "a" (val), "d" (port));
     147        asm volatile (
     148                "outb %b[val], %w[port]\n"
     149                :: [val] "a" (val), [port] "d" (port)
     150        );
    124151}
    125152
     
    130157 * @param port Port to write to
    131158 * @param val Value to write
     159 *
    132160 */
    133161static inline void pio_write_16(ioport16_t *port, uint16_t val)
    134162{
    135         asm volatile ("outw %w0, %w1\n" : : "a" (val), "d" (port));
     163        asm volatile (
     164                "outw %w[val], %w[port]\n"
     165                :: [val] "a" (val), [port] "d" (port)
     166        );
    136167}
    137168
     
    142173 * @param port Port to write to
    143174 * @param val Value to write
     175 *
    144176 */
    145177static inline void pio_write_32(ioport32_t *port, uint32_t val)
    146178{
    147         asm volatile ("outl %0, %w1\n" : : "a" (val), "d" (port));
     179        asm volatile (
     180                "outl %[val], %w[port]\n"
     181                :: [val] "a" (val), [port] "d" (port)
     182        );
    148183}
    149184
     
    160195 *
    161196 * @return Old interrupt priority level.
     197 *
    162198 */
    163199static inline ipl_t interrupts_enable(void) {
    164200        ipl_t v;
    165         __asm__ volatile (
     201       
     202        asm volatile (
    166203                "pushfq\n"
    167                 "popq %0\n"
     204                "popq %[v]\n"
    168205                "sti\n"
    169                 : "=r" (v)
    170         );
     206                : [v] "=r" (v)
     207        );
     208       
    171209        return v;
    172210}
     
    178216 *
    179217 * @return Old interrupt priority level.
     218 *
    180219 */
    181220static inline ipl_t interrupts_disable(void) {
    182221        ipl_t v;
    183         __asm__ volatile (
     222       
     223        asm volatile (
    184224                "pushfq\n"
    185                 "popq %0\n"
     225                "popq %[v]\n"
    186226                "cli\n"
    187                 : "=r" (v)
    188                 );
     227                : [v] "=r" (v)
     228        );
     229       
    189230        return v;
    190231}
     
    195236 *
    196237 * @param ipl Saved interrupt priority level.
     238 *
    197239 */
    198240static inline void interrupts_restore(ipl_t ipl) {
    199         __asm__ volatile (
    200                 "pushq %0\n"
     241        asm volatile (
     242                "pushq %[ipl]\n"
    201243                "popfq\n"
    202                 : : "r" (ipl)
    203                 );
     244                :: [ipl] "r" (ipl)
     245        );
    204246}
    205247
     
    209251 *
    210252 * @return Current interrupt priority level.
     253 *
    211254 */
    212255static inline ipl_t interrupts_read(void) {
    213256        ipl_t v;
    214         __asm__ volatile (
     257       
     258        asm volatile (
    215259                "pushfq\n"
    216                 "popq %0\n"
    217                 : "=r" (v)
    218         );
     260                "popq %[v]\n"
     261                : [v] "=r" (v)
     262        );
     263       
    219264        return v;
    220265}
     
    223268static inline void write_msr(uint32_t msr, uint64_t value)
    224269{
    225         __asm__ volatile (
    226                 "wrmsr;" : : "c" (msr),
    227                 "a" ((uint32_t)(value)),
    228                 "d" ((uint32_t)(value >> 32))
    229                 );
     270        asm volatile (
     271                "wrmsr\n"
     272                :: "c" (msr),
     273                   "a" ((uint32_t) (value)),
     274                   "d" ((uint32_t) (value >> 32))
     275        );
    230276}
    231277
     
    233279{
    234280        uint32_t ax, dx;
    235 
    236         __asm__ volatile (
    237                 "rdmsr;" : "=a"(ax), "=d"(dx) : "c" (msr)
    238                 );
    239         return ((uint64_t)dx << 32) | ax;
     281       
     282        asm volatile (
     283                "rdmsr\n"
     284                : "=a" (ax), "=d" (dx)
     285                : "c" (msr)
     286        );
     287       
     288        return ((uint64_t) dx << 32) | ax;
    240289}
    241290
     
    244293 *
    245294 * Enable local APIC in MSR.
     295 *
    246296 */
    247297static inline void enable_l_apic_in_msr()
    248298{
    249         __asm__ volatile (
     299        asm volatile (
    250300                "movl $0x1b, %%ecx\n"
    251301                "rdmsr\n"
    252                 "orl $(1<<11),%%eax\n"
     302                "orl $(1 << 11),%%eax\n"
    253303                "orl $(0xfee00000),%%eax\n"
    254304                "wrmsr\n"
    255                 :
    256                 :
    257                 :"%eax","%ecx","%edx"
    258                 );
     305                ::: "%eax","%ecx","%edx"
     306        );
    259307}
    260308
     
    262310{
    263311        uintptr_t *ip;
    264 
    265         __asm__ volatile (
    266                 "mov %%rip, %0"
    267                 : "=r" (ip)
    268                 );
     312       
     313        asm volatile (
     314                "mov %%rip, %[ip]"
     315                : [ip] "=r" (ip)
     316        );
     317       
    269318        return ip;
    270319}
     
    273322 *
    274323 * @param addr Address on a page whose TLB entry is to be invalidated.
     324 *
    275325 */
    276326static inline void invlpg(uintptr_t addr)
    277327{
    278         __asm__ volatile ("invlpg %0\n" :: "m" (*((unative_t *)addr)));
     328        asm volatile (
     329                "invlpg %[addr]\n"
     330                :: [addr] "m" (*((unative_t *) addr))
     331        );
    279332}
    280333
     
    282335 *
    283336 * @param gdtr_reg Address of memory from where to load GDTR.
     337 *
    284338 */
    285339static inline void gdtr_load(struct ptr_16_64 *gdtr_reg)
    286340{
    287         __asm__ volatile ("lgdtq %0\n" : : "m" (*gdtr_reg));
     341        asm volatile (
     342                "lgdtq %[gdtr_reg]\n"
     343                :: [gdtr_reg] "m" (*gdtr_reg)
     344        );
    288345}
    289346
     
    291348 *
    292349 * @param gdtr_reg Address of memory to where to load GDTR.
     350 *
    293351 */
    294352static inline void gdtr_store(struct ptr_16_64 *gdtr_reg)
    295353{
    296         __asm__ volatile ("sgdtq %0\n" : : "m" (*gdtr_reg));
     354        asm volatile (
     355                "sgdtq %[gdtr_reg]\n"
     356                :: [gdtr_reg] "m" (*gdtr_reg)
     357        );
    297358}
    298359
     
    300361 *
    301362 * @param idtr_reg Address of memory from where to load IDTR.
     363 *
    302364 */
    303365static inline void idtr_load(struct ptr_16_64 *idtr_reg)
    304366{
    305         __asm__ volatile ("lidtq %0\n" : : "m" (*idtr_reg));
     367        asm volatile (
     368                "lidtq %[idtr_reg]\n"
     369                :: [idtr_reg] "m" (*idtr_reg));
    306370}
    307371
     
    309373 *
    310374 * @param sel Selector specifying descriptor of TSS segment.
     375 *
    311376 */
    312377static inline void tr_load(uint16_t sel)
    313378{
    314         __asm__ volatile ("ltr %0" : : "r" (sel));
     379        asm volatile (
     380                "ltr %[sel]"
     381                :: [sel] "r" (sel)
     382        );
    315383}
    316384
    317385#define GEN_READ_REG(reg) static inline unative_t read_ ##reg (void) \
    318     { \
    319         unative_t res; \
    320         __asm__ volatile ("movq %%" #reg ", %0" : "=r" (res) ); \
    321         return res; \
    322     }
     386        { \
     387                unative_t res; \
     388                asm volatile ( \
     389                        "movq %%" #reg ", %[res]" \
     390                        : [res] "=r" (res) \
     391                ); \
     392                return res; \
     393        }
    323394
    324395#define GEN_WRITE_REG(reg) static inline void write_ ##reg (unative_t regn) \
    325     { \
    326         __asm__ volatile ("movq %0, %%" #reg : : "r" (regn)); \
    327     }
     396        { \
     397                asm volatile ( \
     398                        "movq %[regn], %%" #reg \
     399                        :: [regn] "r" (regn) \
     400                ); \
     401        }
    328402
    329403GEN_READ_REG(cr0)
  • kernel/arch/amd64/include/atomic.h

    radd04f7 rf24d300  
    2727 */
    2828
    29 /** @addtogroup amd64   
     29/** @addtogroup amd64
    3030 * @{
    3131 */
     
    4242static inline void atomic_inc(atomic_t *val) {
    4343#ifdef CONFIG_SMP
    44         asm volatile ("lock incq %0\n" : "+m" (val->count));
     44        asm volatile (
     45                "lock incq %[count]\n"
     46                : [count] "+m" (val->count)
     47        );
    4548#else
    46         asm volatile ("incq %0\n" : "+m" (val->count));
     49        asm volatile (
     50                "incq %[count]\n"
     51                : [count] "+m" (val->count)
     52        );
    4753#endif /* CONFIG_SMP */
    4854}
     
    5056static inline void atomic_dec(atomic_t *val) {
    5157#ifdef CONFIG_SMP
    52         asm volatile ("lock decq %0\n" : "+m" (val->count));
     58        asm volatile (
     59                "lock decq %[count]\n"
     60                : [count] "+m" (val->count)
     61        );
    5362#else
    54         asm volatile ("decq %0\n" : "+m" (val->count));
     63        asm volatile (
     64                "decq %[count]\n"
     65                : [count] "+m" (val->count)
     66        );
    5567#endif /* CONFIG_SMP */
    5668}
     
    5971{
    6072        long r = 1;
    61 
     73       
    6274        asm volatile (
    63                 "lock xaddq %1, %0\n"
    64                 : "+m" (val->count), "+r" (r)
     75                "lock xaddq %[r], %[count]\n"
     76                : [count] "+m" (val->count), [r] "+r" (r)
    6577        );
    66 
     78       
    6779        return r;
    6880}
     
    7385       
    7486        asm volatile (
    75                 "lock xaddq %1, %0\n"
    76                 : "+m" (val->count), "+r" (r)
     87                "lock xaddq %[r], %[count]\n"
     88                : [count] "+m" (val->count), [r] "+r" (r)
    7789        );
    7890       
     
    8092}
    8193
    82 #define atomic_preinc(val) (atomic_postinc(val) + 1)
    83 #define atomic_predec(val) (atomic_postdec(val) - 1)
     94#define atomic_preinc(val)  (atomic_postinc(val) + 1)
     95#define atomic_predec(val)  (atomic_postdec(val) - 1)
    8496
    8597static inline uint64_t test_and_set(atomic_t *val) {
     
    8799       
    88100        asm volatile (
    89                 "movq $1, %0\n"
    90                 "xchgq %0, %1\n"
    91                 : "=r" (v), "+m" (val->count)
     101                "movq $1, %[v]\n"
     102                "xchgq %[v], %[count]\n"
     103                : [v] "=r" (v), [count] "+m" (val->count)
    92104        );
    93105       
     
    100112{
    101113        uint64_t tmp;
    102 
     114       
    103115        preemption_disable();
    104116        asm volatile (
     
    107119                "pause\n"
    108120#endif
    109                 "mov %0, %1\n"
    110                 "testq %1, %1\n"
     121                "mov %[count], %[tmp]\n"
     122                "testq %[tmp], %[tmp]\n"
    111123                "jnz 0b\n"       /* lightweight looping on locked spinlock */
    112124               
    113                 "incq %1\n"      /* now use the atomic operation */
    114                 "xchgq %0, %1\n"
    115                 "testq %1, %1\n"
     125                "incq %[tmp]\n"  /* now use the atomic operation */
     126                "xchgq %[count], %[tmp]\n"
     127                "testq %[tmp], %[tmp]\n"
    116128                "jnz 0b\n"
    117                 : "+m" (val->count), "=&r" (tmp)
     129                : [count] "+m" (val->count), [tmp] "=&r" (tmp)
    118130        );
    119131        /*
  • kernel/arch/amd64/src/amd64.c

    radd04f7 rf24d300  
    7373static void clean_IOPL_NT_flags(void)
    7474{
    75         asm (
     75        asm volatile (
    7676                "pushfq\n"
    7777                "pop %%rax\n"
     
    7979                "pushq %%rax\n"
    8080                "popfq\n"
    81                 :
    82                 :
    83                 : "%rax"
     81                ::: "%rax"
    8482        );
    8583}
     
    9189static void clean_AM_flag(void)
    9290{
    93         asm (
     91        asm volatile (
    9492                "mov %%cr0, %%rax\n"
    9593                "and $~(0x40000), %%rax\n"
    9694                "mov %%rax, %%cr0\n"
    97                 :
    98                 :
    99                 : "%rax"
     95                ::: "%rax"
    10096        );
    10197}
  • kernel/arch/amd64/src/cpu/cpu.c

    radd04f7 rf24d300  
    7878{
    7979        asm volatile (
    80                 "movq %%cr0, %%rax;"
    81                 "btsq $1, %%rax;" /* cr0.mp */
    82                 "btrq $2, %%rax;"  /* cr0.em */
    83                 "movq %%rax, %%cr0;"
    84 
    85                 "movq %%cr4, %%rax;"
    86                 "bts $9, %%rax;" /* cr4.osfxsr */
    87                 "movq %%rax, %%cr4;"
    88                 :
    89                 :
    90                 :"%rax"
    91                 );
     80                "movq %%cr0, %%rax\n"
     81                "btsq $1, %%rax\n"  /* cr0.mp */
     82                "btrq $2, %%rax\n"  /* cr0.em */
     83                "movq %%rax, %%cr0\n"
     84               
     85                "movq %%cr4, %%rax\n"
     86                "bts $9, %%rax\n"   /* cr4.osfxsr */
     87                "movq %%rax, %%cr4\n"
     88                ::: "%rax"
     89        );
    9290}
    9391
    94 /** Set the TS flag to 1. 
     92/** Set the TS flag to 1.
    9593 *
    9694 * If a thread accesses coprocessor, exception is run, which
     
    10098void fpu_disable(void)
    10199{
    102         asm     volatile (
    103                 "mov %%cr0,%%rax;"
    104                 "bts $3,%%rax;"
    105                 "mov %%rax,%%cr0;"
    106                 :
    107                 :
    108                 :"%rax"
    109                 );
     100        asm volatile (
     101                "mov %%cr0, %%rax\n"
     102                "bts $3, %%rax\n"
     103                "mov %%rax, %%cr0\n"
     104                ::: "%rax"
     105        );
    110106}
    111107
    112108void fpu_enable(void)
    113109{
    114         asm     volatile (
    115                 "mov %%cr0,%%rax;"
    116                 "btr $3,%%rax;"
    117                 "mov %%rax,%%cr0;"
    118                 :
    119                 :
    120                 :"%rax"
    121                 );     
     110        asm volatile (
     111                "mov %%cr0, %%rax\n"
     112                "btr $3, %%rax\n"
     113                "mov %%rax, %%cr0\n"
     114                ::: "%rax"
     115        );
    122116}
    123117
  • kernel/arch/amd64/src/fpu_context.c

    radd04f7 rf24d300  
    4040{
    4141        asm volatile (
    42                 "fxsave %0"
    43                 : "=m"(*fctx)
    44                 );
     42                "fxsave %[fctx]\n"
     43                : [fctx] "=m" (*fctx)
     44        );
    4545}
    4646
     
    4949{
    5050        asm volatile (
    51                 "fxrstor %0"
    52                 : "=m"(*fctx)
    53                 );
     51                "fxrstor %[fctx]\n"
     52                : [fctx] "=m" (*fctx)
     53        );
    5454}
    5555
     
    5858        /* TODO: Zero all SSE, MMX etc. registers */
    5959        asm volatile (
    60                 "fninit;"
     60                "fninit\n"
    6161        );
    6262}
  • kernel/arch/amd64/src/userspace.c

    radd04f7 rf24d300  
    2727 */
    2828
    29 /** @addtogroup amd64   
     29/** @addtogroup amd64
    3030 * @{
    3131 */
     
    4848void userspace(uspace_arg_t *kernel_uarg)
    4949{
    50         ipl_t ipl;
     50        ipl_t ipl = interrupts_disable();
    5151       
    52         ipl = interrupts_disable();
    53 
    54         /* Clear CF,PF,AF,ZF,SF,DF,OF */
     52        /* Clear CF, PF, AF, ZF, SF, DF, OF */
    5553        ipl &= ~(0xcd4);
    56 
    57         asm volatile (""
    58                           "pushq %0\n"
    59                           "pushq %1\n"
    60                           "pushq %2\n"
    61                           "pushq %3\n"
    62                           "pushq %4\n"
    63                           "movq %5, %%rax\n"
    64                           /* %rdi is defined to hold pcb_ptr - set it to 0 */
    65                           "xorq %%rdi, %%rdi\n"
    66                           "iretq\n"
    67                           : :
    68                           "i" (gdtselector(UDATA_DES) | PL_USER),
    69                           "r" (kernel_uarg->uspace_stack+THREAD_STACK_SIZE),
    70                           "r" (ipl),
    71                           "i" (gdtselector(UTEXT_DES) | PL_USER),
    72                           "r" (kernel_uarg->uspace_entry),
    73                           "r" (kernel_uarg->uspace_uarg)
    74                           : "rax"
    75                           );
     54       
     55        asm volatile (
     56                        "pushq %[udata_des]\n"
     57                        "pushq %[stack_size]\n"
     58                        "pushq %[ipl]\n"
     59                        "pushq %[utext_des]\n"
     60                        "pushq %[entry]\n"
     61                        "movq %[uarg], %%rax\n"
     62                       
     63                        /* %rdi is defined to hold pcb_ptr - set it to 0 */
     64                        "xorq %%rdi, %%rdi\n"
     65                        "iretq\n"
     66                        :: [udata_des] "i" (gdtselector(UDATA_DES) | PL_USER),
     67                           [stack_size] "r" (kernel_uarg->uspace_stack + THREAD_STACK_SIZE),
     68                           [ipl] "r" (ipl),
     69                           [utext_des] "i" (gdtselector(UTEXT_DES) | PL_USER),
     70                           [entry] "r" (kernel_uarg->uspace_entry),
     71                           [uarg] "r" (kernel_uarg->uspace_uarg)
     72                        : "rax"
     73                );
    7674       
    7775        /* Unreachable */
    78         for(;;)
    79                 ;
     76        while (1);
    8077}
    8178
Note: See TracChangeset for help on using the changeset viewer.