Changeset 339


Ignore:
Timestamp:
Aug 7, 2017, 3:11:45 PM (7 years ago)
Author:
max@…
Message:

Add support for context switch - not tested yet, due to some other bugs in
the mapper. This cswitch is similar to that of TSAR.

Location:
trunk/hal/x86_64/core
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/hal/x86_64/core/hal_context.c

    r335 r339  
    3939        hal_trapframe_t *tf;
    4040        hal_cpu_context_t *ctx;
     41        uint64_t kstacktop;
    4142        kmem_req_t req;
     43
     44        kstacktop = (uint64_t)thread->k_stack_base + thread->k_stack_size;
    4245
    4346        /* allocate memory for cpu_context */
     
    5659         * Build the context
    5760         */
    58         ctx->ctx_rsp0 = ((uint64_t)thread->k_stack_base) + thread->k_stack_size;
    59         ctx->ctx_rsp0 &= ~0xF;
    60         /* XXX: ctx_rsp */
     61        ctx->ctx_rsp0 = kstacktop & ~0xF;
     62        ctx->ctx_tf = (uint64_t)&ctx->ctx_hidden_tf;
    6163
    6264        /*
    6365         * Build the trap frame
    6466         */
    65         tf = (void *)(((uint64_t)thread->k_stack_base +
    66             thread->k_stack_size) & ~0xF);
     67        tf = &ctx->ctx_hidden_tf;
    6768        tf->tf_gs = GDT_FIXED_SEL(GDT_UDATA_SEL, SEL_UPL);
    6869        tf->tf_fs = GDT_FIXED_SEL(GDT_UDATA_SEL, SEL_UPL);
     
    7879        } else {
    7980                tf->tf_cs = GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL);
    80                 tf->tf_rsp = ((uint64_t)thread->k_stack_base) +
    81                     thread->k_stack_size;
     81                tf->tf_rsp = kstacktop;
    8282        }
    8383
     
    9999{
    100100        x86_panic((char *)__func__);
     101
     102        /* Switch the VM space */
     103        // TODO
     104
     105        /* Switch the CPU context */
     106        cpu_context_switch(old->cpu_context, new->cpu_context);
    101107}
    102108
  • trunk/hal/x86_64/core/hal_cpu.S

    r309 r339  
    2424#include <hal_segmentation.h>
    2525#include <hal_register.h>
     26#include <hal_kentry.h>
    2627
    2728ASM_ENTRY(lgdt)
     
    181182/* -------------------------------------------------------------------------- */
    182183
     184ASM_ENTRY(cpu_context_switch)
     185        pushq   %r12
     186        pushq   %r13
     187
     188        movq    %rdi,%r13       /* oldctx */
     189        movq    %rsi,%r12       /* newctx */
     190
     191        /*
     192         * Save the current stack in %rdx, and switch to the trap frame of
     193         * the old thread.
     194         */
     195        movq    %rsp,%rdx
     196        movq    CTX_TF(%r13),%rsp
     197        addq    $TF_SIZE,%rsp   /* end of the structure */
     198
     199        /* Build the trap frame */
     200        movl    %ss,%eax
     201        pushq   %rax            /* tf_ss */
     202        pushq   %rdx            /* tf_rsp */
     203        pushfq                  /* tf_rflags */
     204        movl    %cs,%eax
     205        pushq   %rax            /* tf_cs */
     206        movabsq $thr_resume,%rax
     207        pushq   %rax            /* tf_rip */
     208        pushq   $0              /* tf_err */
     209        pushq   $T_ASTFLT       /* tf_trapno */
     210        INTR_SAVE_REGS
     211
     212        /* Switch rsp0 */
     213        movq    CTX_RSP0(%r12),%rax
     214        movq    TLSVAR(RSP0),%rdx
     215        movq    %rax,(%rdx)
     216
     217        /* Switch to the new trap frame */
     218        movq    CTX_TF(%r12),%rsp
     219
     220        /*
     221         * Restore the context, and jump into the new thread.
     222         */
     223        INTR_RESTORE_REGS
     224        iretq
     225
     226thr_resume:
     227        /*
     228         * Only pop %r12 and %r13, and return.
     229         */
     230        popq    %r13
     231        popq    %r12
     232
     233        ret
     234
     235/* -------------------------------------------------------------------------- */
     236
    183237ASM_ENTRY(atomic_cas_32)
    184238        movl    %esi,%eax
  • trunk/hal/x86_64/core/hal_init.c

    r323 r339  
    654654        cputls->tls_gid = hal_lapic_gid();
    655655        cputls->tls_lid = lid;
     656        /* cputls->tls_rsp0 = (uint64_t)&data->tss.tss_rsp0; */
    656657        cputls->tls_intr = INTRS_DISABLED;
    657658
  • trunk/hal/x86_64/core/hal_internal.h

    r309 r339  
    6161void wbinvd();
    6262
     63void cpu_context_switch(void *oldctx, void *newctx);
     64
    6365uint32_t atomic_cas_32(volatile uint32_t *ptr, uint32_t exp, uint32_t new);
    6466uint32_t atomic_add_32(volatile uint32_t *ptr, int32_t incr);
  • trunk/hal/x86_64/core/hal_kentry.h

    r335 r339  
    4545#define T_USER  0x100
    4646
    47 #define CPUVAR(off)     %gs:CPU_INFO_ ## off
     47#define TLSVAR(off)     %gs:TLS_ ## off
    4848
    4949/*
     
    128128
    129129/*
    130  * The x86_64 CPU trap frame.
     130 * The x86_64 CPU trap frame. !!WARNING!! The size of this structure must be
     131 * exactly TF_SIZE.
    131132 */
    132133typedef struct hal_trapframe_s {
     
    168169typedef struct hal_cpu_context_s {
    169170        uint64_t ctx_rsp0;
    170         uint64_t ctx_rsp;
    171         uint64_t ctx_rbp;
     171        uint64_t ctx_tf;
     172        hal_trapframe_t ctx_hidden_tf;
    172173} hal_cpu_context_t;
    173174
    174175#else
    175176
    176 /* offsets in the trapframe structure */
     177/* Offsets in the trapframe structure */
    177178#define TF_RAX  0
    178179#define TF_RBX  8
     
    195196#define TF_DS   144
    196197
     198/* Size of the trapframe structure */
     199#define TF_SIZE 208
     200
     201/* Offsets in the context structure */
     202#define CTX_RSP0  0
     203#define CTX_TF    8
     204
    197205#endif
    198206
  • trunk/hal/x86_64/core/hal_segmentation.h

    r336 r339  
    154154        uint32_t tls_gid;
    155155        uint32_t tls_lid;
     156        uint64_t tls_rsp0; /* pointer for fast access */
    156157        void *tls_thr;
    157158        reg_t tls_intr;
    158         void *tls_tf;
     159        void *tls_tf;     /* debug only */
    159160} __packed;
    160161typedef struct tls tls_t;
     
    169170
    170171#endif /* !x86_ASM */
     172
     173/* TLS offsets */
     174#define TLS_SELF        0
     175#define TLS_GID         8
     176#define TLS_LID         12
     177#define TLS_RSP0        16
    171178
    172179/* system segments and gate types */
Note: See TracChangeset for help on using the changeset viewer.