📄 kernel.c
字号:
/* LEON2/3 LIBIO low-level routines Written by Jiri Gaisler. Copyright (C) 2004 Gaisler Research AB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/#include <sys/fsu_pthread_queue.h>#include <asm-leon/contextswitch.h>#include <asm-leon/leonbare_kernel.h>#include <asm-leon/leonbare_debug.h>#include <asm-leon/stack.h>#include <asm-leon/leonstack.h>#include <asm-leon/irq.h>struct leonbare_kernel leonbare_kernel;/* * The kernels runqueues are 2 way sorted. First there are n queues for different * priority threads. Second each queue is sorted according to the th_account field * of the thread that is decremented on each leonbare_sched_tick() for the thread * that is currently active. The thread that has consuments it's th_account is * removed from its queue and reinserted only later when leonbare_sched_paytime() * is called when all queues are empty. After leonbare_sched_tick() is called the * next thread to execute is the first thread in the first queue 0..n not empty. * queue 0: [ <acc=2>], * queue 1: [ <acc=10>, <acc=8>,<acc=8>,<acc=1> ], * queue 2: [ ... ], * ... * queue n: [ ... ] */int leonbare_sched_tick() { volatile leonbare_thread_t c = LEONBARE_KR_CURRENT; leonbare_thread_t i; LBDEBUG_FNCALL; leonbare_sched_verify(); if (c) { if ((--c->th_account) < 0) { LBDEBUG_HEADER_PRINTF(LBDEBUG_QUEUE_NR,"remove %s(%x)\n",LEONBARE_TH_NAME_DBG(c),c); TAILQ_REMOVE(LEONBARE_KR_RUNQ(c->th_runq_idx),c,th_runq[c->th_runq_idx]); c->th_runq_idx = -1; } } else { if (TAILQ_NEXT(c,th_runq[c->th_runq_idx])) { if (c->th_account < TAILQ_NEXT(c,th_runq[c->th_runq_idx])->th_account) { TAILQ_REMOVE(LEONBARE_KR_RUNQ(c->th_runq_idx),c,th_runq[c->th_runq_idx]); TAILQ_FOREACH(i, LEONBARE_KR_RUNQ(c->th_runq_idx), th_runq[c->th_runq_idx]) { if (i->th_account > c->th_account) { TAILQ_INSERT_BEFORE(i,c,th_runq[c->th_runq_idx]); break; } } if (!i) { TAILQ_INSERT_TAIL(LEONBARE_KR_RUNQ(c->th_runq_idx),c,th_runq[c->th_runq_idx]); } } } } if (!(LEONBARE_KR_NEXT = leonbare_sched_next())) { LEONBARE_KR_NEXT = leonbare_sched_paytime(); } leonbare_sched_verify(); leonbare_sched_printqueue(); LBDEBUG_FNEXIT; return (LEONBARE_KR_CURRENT != LEONBARE_KR_NEXT);}leonbare_thread_t leonbare_sched_paytime() { leonbare_thread_t i; leonbare_sched_verify(); LBDEBUG_FNCALL; TAILQ_FOREACH(i, LEONBARE_KR_ALLQ, th_allq) { i->th_account = 1; leonbare_sched_insert(i); } LBDEBUG_FNEXIT; return leonbare_sched_next();}void leonbare_sched_insert(leonbare_thread_t thread) { leonbare_thread_t i = 0; LBDEBUG_FNCALL; if (thread->th_pri_idx != -1) { thread->th_runq_idx = thread->th_pri_idx; TAILQ_FOREACH(i, LEONBARE_KR_RUNQ(thread->th_runq_idx), th_runq[thread->th_runq_idx]) { if (i->th_account > thread->th_account) { TAILQ_INSERT_BEFORE(i,thread,th_runq[thread->th_runq_idx]); break; } } if (!i) { TAILQ_INSERT_TAIL(LEONBARE_KR_RUNQ(thread->th_runq_idx),thread,th_runq[thread->th_runq_idx]); } } LBDEBUG_FNEXIT;}void leonbare_sched() { volatile leonbare_thread_t old = LEONBARE_KR_CURRENT, new = LEONBARE_KR_NEXT; LBDEBUG_FNCALL; //LEONBARE_KR_CURRENT = new; //LEONBARE_REENT_SET(new); LBDEBUG_HEADER_PRINTF(LBDEBUG_SCHED_NR,"switch %s[%x]->%s[%x]\n",LEONBARE_TH_NAME_DBG(old),old,LEONBARE_TH_NAME_DBG(new),new); LEONBARE_KR_CURRENT = new; RESTORE_CONTEXT(new); /* never come here*/ __asm__ __volatile__ (" ta 0; ");}void _leonbare_thread_body() { LBDEBUG_FNCALL; leonbare_thread_t thread = LEONBARE_KR_CURRENT; LEONBARE_KR_IS_IN_KERNEL = 0; thread->th_result = thread->th_func(thread->th_arg); LBDEBUG_FNEXIT;}#define LEONBARE_BODY_OFFSET 200int leonbare_thread_create(leonbare_thread_t thread, char *stack, int stacksize) { struct sparc_stackframe_regs *sp; unsigned int fpspill = 0; LBDEBUG_FNCALL; thread->th_stack_base = (char *)LEONBARE_STACKALIGN(stack); stacksize -= thread->th_stack_base - stack; thread->th_stack_size = stacksize; thread->th_runq_idx = 0; thread->th_pri_idx = 0; thread->th_account = 0; /* stack */ sp = (struct sparc_stackframe_regs *)(thread->th_stack_base + LEONBARE_STACKALIGN(thread->th_stack_size - LEONBARE_BODY_OFFSET - ((WINDOWSIZE*2) + FW_REGS_SZ))); fpspill = ((unsigned int)sp) + (WINDOWSIZE*2); thread_setjmp(thread->th_ctx, 0); thread->th_ctx[THREAD_JB_FPUCTX] = fpspill; thread->th_ctx[THREAD_JB_SP] = (int) sp; thread->th_ctx[THREAD_JB_PC] = (int) _leonbare_thread_body; thread->th_ctx[THREAD_JB_PC] -= 8; sp->sf_ins[6] = ((int) sp) + WINDOWSIZE; /* fp */ /* newlibc reent */ thread->th_reentp = &(thread->th_reent); _REENT_INIT_PTR(thread->th_reentp); /* queues */ TAILQ_INSERT_TAIL(LEONBARE_KR_ALLQ,thread,th_allq); LBDEBUG_FNEXIT;} int leon_bare_do_schedule(struct leonbare_pt_regs *ptregs) { LBDEBUG_FNCALL; if (!LEONBARE_KR_IS_IN_KERNEL) { KERNEL_ENTER; LEONBARE_KR_NEED_SCHEDULE = 1; KERNEL_EXIT; } LBDEBUG_FNEXIT;}struct leonbare_thread _thread_main;int leonbare_thread_init() { int i; LBDEBUG_FNCALL; leonbare_init_ticks(); schedule_callback = leon_bare_do_schedule; _thread_main.th_reentp = _impure_ptr; _thread_main.th_name = "<main>"; _thread_main.th_runq_idx = 0; _thread_main.th_pri_idx = 0; _thread_main.th_account = 0; TAILQ_INIT(LEONBARE_KR_ALLQ); for(i = 0; i < LEONBARE_RUNQ_NR;i++) { TAILQ_INIT(LEONBARE_KR_RUNQ(i)); } /* queues */ TAILQ_INSERT_TAIL(LEONBARE_KR_ALLQ,&_thread_main,th_allq); TAILQ_INSERT_TAIL(LEONBARE_KR_RUNQ(_thread_main.th_runq_idx),&_thread_main,th_runq[_thread_main.th_runq_idx]); LEONBARE_KR_CURRENT = &_thread_main; LEONBARE_KR_IS_IN_KERNEL = 0; LBDEBUG_FNEXIT;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -