⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread.c

📁 Simple Operating Systems (简称SOS)是一个可以运行在X86平台上(包括QEMU
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Copyright (C) 2004,2005 David Decotigny   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 <sos/physmem.h>#include <sos/kmem_slab.h>#include <sos/kmalloc.h>#include <sos/klibc.h>#include <sos/list.h>#include <sos/assert.h>#include <hwcore/mm_context.h>#include <sos/process.h>#include <drivers/bochs.h>#include <drivers/x86_videomem.h>#include <hwcore/irq.h>#include "thread.h"/** * The size of the stack of a kernel thread */#define SOS_THREAD_KERNEL_STACK_SIZE (1*SOS_PAGE_SIZE)/** * The identifier of the thread currently running on CPU. * * We only support a SINGLE processor, ie a SINGLE thread * running at any time in the system. This greatly simplifies the * implementation of the system, since we don't have to complicate * things in order to retrieve the identifier of the threads running * on the CPU. On multiprocessor systems the current_thread below is * an array indexed by the id of the CPU, so that the challenge is to * retrieve the identifier of the CPU. This is usually done based on * the stack address (Linux implementation) or on some form of TLS * ("Thread Local Storage": can be implemented by way of LDTs for the * processes, accessed through the fs or gs registers). */static volatile struct sos_thread *current_thread = NULL;/* * The list of threads currently in the system. * * @note We could have used current_thread for that... */static struct sos_thread *thread_list = NULL;/** * The Cache of thread structures */static struct sos_kslab_cache *cache_thread;/** * (Forwad declaration) Helper function to change the MMU config of * the current executing thread. Analogous to function * sos_thread_change_current_mm_context() of article 7 */static sos_ret_t change_current_mm_context(struct sos_mm_context *mm_ctxt);struct sos_thread *sos_thread_get_current(){  SOS_ASSERT_FATAL(current_thread->state == SOS_THR_RUNNING);  return (struct sos_thread*)current_thread;}inline static sos_ret_t _set_current(struct sos_thread *thr){  SOS_ASSERT_FATAL(thr->state == SOS_THR_READY);  current_thread = thr;  current_thread->state = SOS_THR_RUNNING;  return SOS_OK;}sos_ret_t sos_thread_subsystem_setup(sos_vaddr_t init_thread_stack_base_addr,				     sos_size_t init_thread_stack_size){  struct sos_thread *myself;  /* Allocate the cache of threads */  cache_thread = sos_kmem_cache_create("thread",				       sizeof(struct sos_thread),				       2,				       0,				       SOS_KSLAB_CREATE_MAP				       | SOS_KSLAB_CREATE_ZERO);  if (! cache_thread)    return -SOS_ENOMEM;  /* Allocate a new thread structure for the current running thread */  myself = (struct sos_thread*) sos_kmem_cache_alloc(cache_thread,						     SOS_KSLAB_ALLOC_ATOMIC);  if (! myself)    return -SOS_ENOMEM;  /* Initialize the thread attributes */  strzcpy(myself->name, "[kinit]", SOS_THR_MAX_NAMELEN);  myself->state           = SOS_THR_CREATED;  myself->priority        = SOS_SCHED_PRIO_LOWEST;  myself->kernel_stack_base_addr = init_thread_stack_base_addr;  myself->kernel_stack_size      = init_thread_stack_size;  /* Do some stack poisoning on the bottom of the stack, if needed */  sos_cpu_state_prepare_detect_kernel_stack_overflow(myself->cpu_state,						     myself->kernel_stack_base_addr,						     myself->kernel_stack_size);  /* Add the thread in the global list */  list_singleton_named(thread_list, myself, gbl_prev, gbl_next);  /* Ok, now pretend that the running thread is ourselves */  myself->state = SOS_THR_READY;  _set_current(myself);  return SOS_OK;}struct sos_thread *sos_create_kernel_thread(const char *name,			 sos_kernel_thread_start_routine_t start_func,			 void *start_arg,			 sos_sched_priority_t priority){  __label__ undo_creation;  sos_ui32_t flags;  struct sos_thread *new_thread;  if (! start_func)    return NULL;  if (! SOS_SCHED_PRIO_IS_VALID(priority))    return NULL;  /* Allocate a new thread structure for the current running thread */  new_thread    = (struct sos_thread*) sos_kmem_cache_alloc(cache_thread,						SOS_KSLAB_ALLOC_ATOMIC);  if (! new_thread)    return NULL;  /* Initialize the thread attributes */  strzcpy(new_thread->name, ((name)?name:"[NONAME]"), SOS_THR_MAX_NAMELEN);  new_thread->state    = SOS_THR_CREATED;  new_thread->priority = priority;  /* Allocate the stack for the new thread */  new_thread->kernel_stack_base_addr = sos_kmalloc(SOS_THREAD_KERNEL_STACK_SIZE, 0);  new_thread->kernel_stack_size      = SOS_THREAD_KERNEL_STACK_SIZE;  if (! new_thread->kernel_stack_base_addr)    goto undo_creation;  /* Initialize the CPU context of the new thread */  if (SOS_OK      != sos_cpu_kstate_init(& new_thread->cpu_state,			     (sos_cpu_kstate_function_arg1_t*) start_func,			     (sos_ui32_t) start_arg,			     new_thread->kernel_stack_base_addr,			     new_thread->kernel_stack_size,			     (sos_cpu_kstate_function_arg1_t*) sos_thread_exit,			     (sos_ui32_t) NULL))    goto undo_creation;  /* Add the thread in the global list */  sos_disable_IRQs(flags);  list_add_tail_named(thread_list, new_thread, gbl_prev, gbl_next);  sos_restore_IRQs(flags);  /* Mark the thread ready */  if (SOS_OK != sos_sched_set_ready(new_thread))    goto undo_creation;  /* Normal non-erroneous end of function */  return new_thread; undo_creation:  if (new_thread->kernel_stack_base_addr)    sos_kfree((sos_vaddr_t) new_thread->kernel_stack_base_addr);  sos_kmem_cache_free((sos_vaddr_t) new_thread);  return NULL;}/** * Helper function to create a new user thread. If model_thread is * given, then the new thread will be the copy of this * thread. Otherwise the thread will have its initial SP/PC correctly * initialized with the user_initial_PC/SP arguments */static struct sos_thread *create_user_thread(const char *name,		   struct sos_process *process,		   const struct sos_thread * model_thread,		   const struct sos_cpu_state * model_uctxt,		   sos_uaddr_t user_initial_PC,		   sos_ui32_t  user_start_arg1,		   sos_ui32_t  user_start_arg2,		   sos_uaddr_t user_initial_SP,		   sos_sched_priority_t priority){  __label__ undo_creation;  sos_ui32_t flags;  struct sos_thread *new_thread;  if (model_thread)    {      SOS_ASSERT_FATAL(model_uctxt);    }  else    {      if (! SOS_SCHED_PRIO_IS_VALID(priority))	return NULL;    }  /* For a user thread, the process must be given */  if (! process)    return NULL;  /* Allocate a new thread structure for the current running thread */  new_thread    = (struct sos_thread*) sos_kmem_cache_alloc(cache_thread,						SOS_KSLAB_ALLOC_ATOMIC);  if (! new_thread)    return NULL;  /* Initialize the thread attributes */  strzcpy(new_thread->name, ((name)?name:"[NONAME]"), SOS_THR_MAX_NAMELEN);  new_thread->state    = SOS_THR_CREATED;  if (model_thread)    new_thread->priority = model_thread->priority;  else    new_thread->priority = priority;  /* Allocate the stack for the new thread */  new_thread->kernel_stack_base_addr = sos_kmalloc(SOS_THREAD_KERNEL_STACK_SIZE, 0);  new_thread->kernel_stack_size      = SOS_THREAD_KERNEL_STACK_SIZE;  if (! new_thread->kernel_stack_base_addr)    goto undo_creation;  /* Initialize the CPU context of the new thread */  if (model_thread)    {      if (SOS_OK	  != sos_cpu_ustate_duplicate(& new_thread->cpu_state,				      model_uctxt,				      user_start_arg1,				      new_thread->kernel_stack_base_addr,				      new_thread->kernel_stack_size))	goto undo_creation;    }  else    {      if (SOS_OK	  != sos_cpu_ustate_init(& new_thread->cpu_state,				 user_initial_PC,				 user_start_arg1,				 user_start_arg2,				 user_initial_SP,				 new_thread->kernel_stack_base_addr,				 new_thread->kernel_stack_size))	goto undo_creation;    }  /* Attach the new thread to the process */  if (SOS_OK != sos_process_register_thread(process, new_thread))    goto undo_creation;  /* Add the thread in the global list */  sos_disable_IRQs(flags);  list_add_tail_named(thread_list, new_thread, gbl_prev, gbl_next);  sos_restore_IRQs(flags);  /* Mark the thread ready */  if (SOS_OK != sos_sched_set_ready(new_thread))    goto undo_creation;  /* Normal non-erroneous end of function */  return new_thread; undo_creation:  if (new_thread->kernel_stack_base_addr)    sos_kfree((sos_vaddr_t) new_thread->kernel_stack_base_addr);  sos_kmem_cache_free((sos_vaddr_t) new_thread);  return NULL;}struct sos_thread *sos_create_user_thread(const char *name,		       struct sos_process *process,		       sos_uaddr_t user_initial_PC,		       sos_ui32_t  user_start_arg1,		       sos_ui32_t  user_start_arg2,		       sos_uaddr_t user_initial_SP,		       sos_sched_priority_t priority){  return create_user_thread(name, process, NULL, NULL,			    user_initial_PC,			    user_start_arg1,			    user_start_arg2,			    user_initial_SP,			    priority);}/** * Create a new user thread, copy of the given user thread with the * given user context */struct sos_thread *sos_duplicate_user_thread(const char *name,			  struct sos_process *process,			  const struct sos_thread * model_thread,			  const struct sos_cpu_state * model_uctxt,			  sos_ui32_t retval){  return create_user_thread(name, process, model_thread, model_uctxt,			    0, retval, 0, 0, 0);}/** * Helper function to switch to the correct MMU configuration to suit * the_thread's needs. *   - When switching to a user-mode thread, force the reconfiguration *     of the MMU *   - When switching to a kernel-mode thread, only change the MMU *     configuration if the thread was squatting someone else's space */static void _prepare_mm_context(struct sos_thread *the_thread){  /* Going to restore a thread in user mode ? */  if (sos_cpu_context_is_in_user_mode(the_thread->cpu_state)      == TRUE)    {      /* Yes: force the MMU to be correctly setup with the correct	 user's address space */      /* The thread should be a user thread */      SOS_ASSERT_FATAL(the_thread->process != NULL);      /* It should not squat any other's address space */      SOS_ASSERT_FATAL(the_thread->squatted_mm_context == NULL);      /* Perform an MMU context switch if needed */      sos_mm_context_switch_to(sos_process_get_mm_context(the_thread->process));    }  /* the_thread is a kernel thread squatting a precise address     space ? */  else if (the_thread->squatted_mm_context != NULL)    sos_mm_context_switch_to(the_thread->squatted_mm_context);}/** Function called after thr has terminated. Called from inside the context    of another thread, interrupts disabled */static void delete_thread(struct sos_thread *thr){  sos_ui32_t flags;  sos_disable_IRQs(flags);  list_delete_named(thread_list, thr, gbl_prev, gbl_next);  sos_restore_IRQs(flags);  sos_kfree((sos_vaddr_t) thr->kernel_stack_base_addr);  /* If the thread squats an address space, release it */  if (thr->squatted_mm_context)    SOS_ASSERT_FATAL(SOS_OK == change_current_mm_context(NULL));  /* For a user thread: remove the thread from the process threads' list */  if (thr->process)    SOS_ASSERT_FATAL(SOS_OK == sos_process_unregister_thread(thr));  memset(thr, 0x0, sizeof(struct sos_thread));  sos_kmem_cache_free((sos_vaddr_t) thr);}void sos_thread_exit(){  sos_ui32_t flags;  struct sos_thread *myself, *next_thread;  /* Interrupt handlers are NOT allowed to exit the current thread ! */  SOS_ASSERT_FATAL(! sos_servicing_irq());  myself = sos_thread_get_current();  /* Refuse to end the current executing thread if it still holds a     resource ! */  SOS_ASSERT_FATAL(list_is_empty_named(myself->kwaitq_list,				       prev_entry_for_thread,				       next_entry_for_thread));  /* Prepare to run the next thread */  sos_disable_IRQs(flags);  myself->state = SOS_THR_ZOMBIE;  next_thread = sos_reschedule(myself, FALSE);  /* Make sure that the next_thread is valid */  sos_cpu_state_detect_kernel_stack_overflow(next_thread->cpu_state,					     next_thread->kernel_stack_base_addr,					     next_thread->kernel_stack_size);  /*   * Perform an MMU context switch if needed   */  _prepare_mm_context(next_thread);  /* No need for sos_restore_IRQs() here because the IRQ flag will be     restored to that of the next thread upon context switch */  /* Immediate switch to next thread */  _set_current(next_thread);  sos_cpu_context_exit_to(next_thread->cpu_state,			  (sos_cpu_kstate_function_arg1_t*) delete_thread,			  (sos_ui32_t) myself);}sos_sched_priority_t sos_thread_get_priority(struct sos_thread *thr){  if (! thr)    thr = (struct sos_thread*)current_thread;  return thr->priority;}sos_thread_state_t sos_thread_get_state(struct sos_thread *thr){  if (! thr)    thr = (struct sos_thread*)current_thread;  return thr->state;}typedef enum { YIELD_MYSELF, BLOCK_MYSELF } switch_type_t;/** * Helper function to initiate a context switch in case the current * thread becomes blocked, waiting for a timeout, or calls yield. */static sos_ret_t _switch_to_next_thread(switch_type_t operation){  struct sos_thread *myself, *next_thread;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -