Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 128359eb in mainline


Ignore:
Timestamp:
2020-06-12T16:46:32Z (3 weeks ago)
Author:
Martin Decky <martin@…>
Branches:
master
Children:
07cdb11, ffccdff0
Parents:
94e75cf
Message:

Replace get_stack_base() with builtin_frame_address(0)

The usage of an intrinsic function to obtain the current stack pointer
should provide the compuler more room for performance optimizations than
the hand-written (and volatile) inline assembly block.

Location:
kernel
Files:
19 edited

Legend:

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

    r94e75cf r128359eb  
    188188}
    189189
    190 _NO_TRACE static inline uintptr_t get_stack_base(void)
    191 {
    192         /*
    193          * On real hardware this returns the address of the bottom
    194          * of the current CPU stack. The current_t structure is stored
    195          * on the bottom of stack and this is used to identify the
    196          * current CPU, current task, current thread and current
    197          * address space.
    198          */
    199 
    200         return 0;
    201 }
    202 
    203190#endif
    204191
  • kernel/arch/amd64/include/arch/asm.h

    r94e75cf r128359eb  
    4242
    4343#define IO_SPACE_BOUNDARY       ((void *) (64 * 1024))
    44 
    45 /** Return base address of current stack.
    46  *
    47  * Return the base address of the current stack.
    48  * The stack is assumed to be STACK_SIZE bytes long.
    49  * The stack must start on page boundary.
    50  *
    51  */
    52 _NO_TRACE static inline uintptr_t get_stack_base(void)
    53 {
    54         uintptr_t v;
    55 
    56         asm volatile (
    57             "andq %%rsp, %[v]\n"
    58             : [v] "=r" (v)
    59             : "0" (~((uint64_t) STACK_SIZE - 1))
    60         );
    61 
    62         return v;
    63 }
    6444
    6545_NO_TRACE static inline void cpu_sleep(void)
  • kernel/arch/arm32/include/arch/asm.h

    r94e75cf r128359eb  
    9595}
    9696
    97 /** Return base address of current stack.
    98  *
    99  * Return the base address of the current stack.
    100  * The stack is assumed to be STACK_SIZE bytes long.
    101  * The stack must start on page boundary.
    102  *
    103  */
    104 _NO_TRACE static inline uintptr_t get_stack_base(void)
    105 {
    106         uintptr_t v;
    107 
    108         asm volatile (
    109             "and %[v], sp, %[size]\n"
    110             : [v] "=r" (v)
    111             : [size] "r" (~(STACK_SIZE - 1))
    112         );
    113 
    114         return v;
    115 }
    116 
    11797extern void cpu_halt(void) __attribute__((noreturn));
    11898extern void asm_delay_loop(uint32_t t);
  • kernel/arch/arm32/include/arch/context.h

    r94e75cf r128359eb  
    4242#include <arch/regutils.h>
    4343
    44 /* Put one item onto the stack to support get_stack_base() and align it up. */
     44/* Put one item onto the stack to support CURRENT and align it up. */
    4545#define SP_DELTA  (0 + ALIGN_UP(STACK_ITEM_SIZE, STACK_ALIGNMENT))
    4646
  • kernel/arch/arm64/include/arch/asm.h

    r94e75cf r128359eb  
    5252{
    5353        asm volatile ("wfe");
    54 }
    55 
    56 /** Return base address of current stack.
    57  *
    58  * Return the base address of the current stack.
    59  * The stack is assumed to be STACK_SIZE bytes long.
    60  * The stack must start on page boundary.
    61  */
    62 _NO_TRACE static inline uintptr_t get_stack_base(void)
    63 {
    64         uintptr_t v;
    65 
    66         asm volatile (
    67             "mov %[v], sp\n"
    68             "and %[v], %[v], %[size]\n"
    69             : [v] "=&r" (v)
    70             : [size] "r" (~((uint64_t) STACK_SIZE - 1))
    71         );
    72 
    73         return v;
    7454}
    7555
  • kernel/arch/arm64/include/arch/context.h

    r94e75cf r128359eb  
    4141#include <arch/stack.h>
    4242
    43 /* Put one item onto the stack to support get_stack_base() and align it up. */
     43/* Put one item onto the stack to support CURRENT and align it up. */
    4444#define SP_DELTA  (0 + ALIGN_UP(STACK_ITEM_SIZE, STACK_ALIGNMENT))
    4545
  • kernel/arch/ia32/include/arch/asm.h

    r94e75cf r128359eb  
    348348
    349349#endif /* PROCESSOR_i486 */
    350 
    351 /** Return base address of current stack
    352  *
    353  * Return the base address of the current stack.
    354  * The stack is assumed to be STACK_SIZE bytes long.
    355  * The stack must start on page boundary.
    356  *
    357  */
    358 _NO_TRACE static inline uintptr_t get_stack_base(void)
    359 {
    360         uintptr_t v;
    361 
    362         asm volatile (
    363             "andl %%esp, %[v]\n"
    364             : [v] "=r" (v)
    365             : "0" (~(STACK_SIZE - 1))
    366         );
    367 
    368         return v;
    369 }
    370350
    371351/** Invalidate TLB Entry.
  • kernel/arch/ia32/include/arch/context.h

    r94e75cf r128359eb  
    4545 * First for pop of the saved register, second during ret instruction.
    4646 *
    47  * One item is put onto stack to support get_stack_base().
     47 * One item is put onto stack to support CURRENT.
    4848 */
    4949#define SP_DELTA  (8 + STACK_ITEM_SIZE)
  • kernel/arch/ia64/include/arch/asm.h

    r94e75cf r128359eb  
    160160}
    161161
    162 /** Return base address of current memory stack.
    163  *
    164  * The memory stack is assumed to be STACK_SIZE / 2 long. Note that there is
    165  * also the RSE stack, which takes up the upper half of STACK_SIZE.
    166  * The memory stack must start on page boundary.
    167  */
    168 _NO_TRACE static inline uintptr_t get_stack_base(void)
    169 {
    170         uint64_t value;
    171 
    172         asm volatile (
    173             "mov %[value] = r12"
    174             : [value] "=r" (value)
    175         );
    176 
    177         return (value & (~(STACK_SIZE / 2 - 1)));
    178 }
    179 
    180162/** Return Processor State Register.
    181163 *
  • kernel/arch/ia64/include/arch/context.h

    r94e75cf r128359eb  
    4646 * No need to allocate scratch area.
    4747 *
    48  * One item is put onto the stack to support get_stack_base().
     48 * One item is put onto the stack to support CURRENT.
    4949 */
    5050#define SP_DELTA  (0 + ALIGN_UP(STACK_ITEM_SIZE, STACK_ALIGNMENT))
  • kernel/arch/mips32/include/arch/asm.h

    r94e75cf r128359eb  
    4545}
    4646
    47 /** Return base address of current stack
    48  *
    49  * Return the base address of the current stack.
    50  * The stack is assumed to be STACK_SIZE bytes long.
    51  * The stack must start on page boundary.
    52  *
    53  */
    54 _NO_TRACE static inline uintptr_t get_stack_base(void)
    55 {
    56         uintptr_t base;
    57 
    58         asm volatile (
    59             "and %[base], $29, %[mask]\n"
    60             : [base] "=r" (base)
    61             : [mask] "r" (~(STACK_SIZE - 1))
    62         );
    63 
    64         return base;
    65 }
    66 
    6747_NO_TRACE static inline void pio_write_8(ioport8_t *port, uint8_t v)
    6848{
  • kernel/arch/mips32/include/arch/context.h

    r94e75cf r128359eb  
    4141
    4242/*
    43  * Put one item onto the stack to support get_stack_base() and align it up.
     43 * Put one item onto the stack to support CURRENT and align it up.
    4444 */
    4545#define SP_DELTA  (ABI_STACK_FRAME + ALIGN_UP(STACK_ITEM_SIZE, STACK_ALIGNMENT))
  • kernel/arch/ppc32/include/arch/asm.h

    r94e75cf r128359eb  
    163163}
    164164
    165 /** Return base address of current stack.
    166  *
    167  * Return the base address of the current stack.
    168  * The stack is assumed to be STACK_SIZE bytes long.
    169  * The stack must start on page boundary.
    170  *
    171  */
    172 _NO_TRACE static inline uintptr_t get_stack_base(void)
    173 {
    174         uintptr_t base;
    175 
    176         asm volatile (
    177             "and %[base], %%sp, %[mask]\n"
    178             : [base] "=r" (base)
    179             : [mask] "r" (~(STACK_SIZE - 1))
    180         );
    181 
    182         return base;
    183 }
    184 
    185165_NO_TRACE static inline void cpu_sleep(void)
    186166{
  • kernel/arch/riscv64/include/arch/asm.h

    r94e75cf r128359eb  
    9191}
    9292
    93 _NO_TRACE static inline uintptr_t get_stack_base(void)
    94 {
    95         uintptr_t base;
    96 
    97         asm volatile (
    98             "and %[base], sp, %[mask]\n"
    99             : [base] "=r" (base)
    100             : [mask] "r" (~(STACK_SIZE - 1))
    101         );
    102 
    103         return base;
    104 }
    105 
    10693_NO_TRACE static inline void cpu_sleep(void)
    10794{
  • kernel/arch/sparc64/include/arch/asm.h

    r94e75cf r128359eb  
    382382}
    383383
    384 /** Return base address of current stack.
    385  *
    386  * Return the base address of the current stack.
    387  * The stack is assumed to be STACK_SIZE bytes long.
    388  * The stack must start on page boundary.
    389  *
    390  */
    391 _NO_TRACE static inline uintptr_t get_stack_base(void)
    392 {
    393         uintptr_t unbiased_sp;
    394 
    395         asm volatile (
    396             "add %%sp, %[stack_bias], %[unbiased_sp]\n"
    397             : [unbiased_sp] "=r" (unbiased_sp)
    398             : [stack_bias] "i" (STACK_BIAS)
    399         );
    400 
    401         return ALIGN_DOWN(unbiased_sp, STACK_SIZE);
    402 }
    403 
    404384/** Read Version Register.
    405385 *
  • kernel/generic/include/arch.h

    r94e75cf r128359eb  
    3636#define KERN_ARCH_H_
    3737
    38 #include <arch/asm.h>   /* get_stack_base() */
    3938#include <config.h>
    4039
    41 /*
     40/** Return the current_t structure
     41 *
    4242 * The current_t structure holds pointers to various parts of the current
    4343 * execution state, like running task, thread, address space, etc.
     44 *
     45 * The current_t structure is located at the base address of the current
     46 * stack. The stack is assumed to be STACK_SIZE bytes long. The stack base
     47 * address must be aligned to STACK_SIZE.
     48 *
    4449 */
    45 #define CURRENT  ((current_t * )(get_stack_base()))
     50#define CURRENT \
     51        ((current_t *) (((uintptr_t) __builtin_frame_address(0)) & \
     52            (~((uintptr_t) STACK_SIZE - 1))))
    4653
    47 #define MAGIC                UINT32_C(0xfacefeed)
     54#define MAGIC  UINT32_C(0xfacefeed)
    4855
    4956#define container_check(ctn1, ctn2)  ((ctn1) == (ctn2))
     
    5966struct as;
    6067
    61 /**
     68/** Current structure
     69 *
    6270 * For each possible kernel stack, structure
    6371 * of the following type will be placed at
    6472 * the base address of the stack.
     73 *
    6574 */
    6675typedef struct {
    67         size_t preemption;     /**< Preemption disabled counter and flag. */
    68         struct thread *thread; /**< Current thread. */
    69         struct task *task;     /**< Current task. */
    70         struct cpu *cpu;       /**< Executing cpu. */
    71         struct as *as;         /**< Current address space. */
    72         uint32_t magic;        /**< Magic value */
     76        size_t preemption;      /**< Preemption disabled counter and flag. */
     77        struct thread *thread;  /**< Current thread. */
     78        struct task *task;      /**< Current task. */
     79        struct cpu *cpu;        /**< Executing CPU. */
     80        struct as *as;          /**< Current address space. */
     81        uint32_t magic;         /**< Magic value. */
    7382} current_t;
    7483
     
    8998        } while (0)
    9099
    91 #define ARCH_OP(op)     ARCH_STRUCT_OP(arch_ops, op)
     100#define ARCH_OP(op)  ARCH_STRUCT_OP(arch_ops, op)
    92101
    93102extern void current_initialize(current_t *);
  • kernel/generic/src/cpu/cpu.c

    r94e75cf r128359eb  
    7171                memsetb(cpus, sizeof(cpu_t) * config.cpu_count, 0);
    7272
    73                 // NOTE: All kernel stacks must be aligned to STACK_SIZE,
    74                 //       see get_stack_base().
    75                 size_t i;
    76                 for (i = 0; i < config.cpu_count; i++) {
     73                /*
     74                 * NOTE: All kernel stacks must be aligned to STACK_SIZE,
     75                 *       see CURRENT.
     76                 */
     77                for (size_t i = 0; i < config.cpu_count; i++) {
    7778                        uintptr_t stack_phys = frame_alloc(STACK_FRAMES,
    7879                            FRAME_LOWMEM | FRAME_ATOMIC, STACK_SIZE - 1);
  • kernel/generic/src/main/main.c

    r94e75cf r128359eb  
    170170            ALIGN_UP((uintptr_t) kdata_end - config.base, PAGE_SIZE);
    171171
    172         // NOTE: All kernel stacks must be aligned to STACK_SIZE,
    173         //       see get_stack_base().
     172        /*
     173         * NOTE: All kernel stacks must be aligned to STACK_SIZE,
     174         *       see CURRENT.
     175         */
    174176
    175177        /* Place the stack after the kernel, init and ballocs. */
  • kernel/generic/src/proc/thread.c

    r94e75cf r128359eb  
    191191        kmflags &= ~FRAME_HIGHMEM;
    192192
    193         // NOTE: All kernel stacks must be aligned to STACK_SIZE,
    194         //       see get_stack_base().
     193        /*
     194         * NOTE: All kernel stacks must be aligned to STACK_SIZE,
     195         *       see CURRENT.
     196         */
    195197
    196198        uintptr_t stack_phys =
Note: See TracChangeset for help on using the changeset viewer.