Exemple #1
0
/*
 * Thread system initialization.
 */
void
thread_bootstrap(void)
{
	struct cpu *bootcpu;
	struct thread *bootthread;

	cpuarray_init(&allcpus);

	/*
	 * Create the cpu structure for the bootup CPU, the one we're
	 * currently running on. Assume the hardware number is 0; that
	 * might be updated later by mainbus-type code. This also
	 * creates a thread structure for the first thread, the one
	 * that's already implicitly running when the kernel is
	 * started from the bootloader.
	 */
	bootcpu = cpu_create(0);
	bootthread = bootcpu->c_curthread;

	/*
	 * Initializing curcpu and curthread is machine-dependent
	 * because either of curcpu and curthread might be defined in
	 * terms of the other.
	 */
	INIT_CURCPU(bootcpu, bootthread);

	/*
	 * Now make sure both t_cpu and c_curthread are set. This
	 * might be partially redundant with INIT_CURCPU depending on
	 * how things are defined.
	 */
	curthread->t_cpu = curcpu;
	curcpu->c_curthread = curthread;

	/* cpu_create() should have set t_proc. */
	KASSERT(curthread->t_proc != NULL);

	/* Initialize allwchans */
	spinlock_init(&allwchans_lock);
	wchanarray_init(&allwchans);

	/* Done */
}
/*
 * Create a CPU structure. This is used for the bootup CPU and
 * also for secondary CPUs.
 *
 * The hardware number (the number assigned by firmware or system
 * board config or whatnot) is tracked separately because it is not
 * necessarily anything sane or meaningful.
 */
struct cpu *
cpu_create(unsigned hardware_number)
{
    struct cpu *c;
    int result;
    char namebuf[16];

    c = kmalloc(sizeof(*c));
    if (c == NULL) {
        panic("cpu_create: Out of memory\n");
    }

    c->c_self = c;
    c->c_hardware_number = hardware_number;

    c->c_curthread = NULL;
    threadlist_init(&c->c_zombies);
    c->c_hardclocks = 0;
    c->c_spinlocks = 0;

    c->c_isidle = false;
    threadlist_init(&c->c_runqueue);
    spinlock_init(&c->c_runqueue_lock);

    c->c_ipi_pending = 0;
    c->c_numshootdown = 0;
    spinlock_init(&c->c_ipi_lock);

    result = cpuarray_add(&allcpus, c, &c->c_number);
    if (result != 0) {
        panic("cpu_create: array_add: %s\n", strerror(result));
    }

    snprintf(namebuf, sizeof(namebuf), "<boot #%d>", c->c_number);
    c->c_curthread = thread_create(namebuf);
    if (c->c_curthread == NULL) {
        panic("cpu_create: thread_create failed\n");
    }
    c->c_curthread->t_cpu = c;

    if (c->c_number == 0) {
        /*
         * Leave c->c_curthread->t_stack NULL for the boot
         * cpu. This means we're using the boot stack, which
         * can't be freed. (Exercise: what would it take to
         * make it possible to free the boot stack?)
         */
        /*c->c_curthread->t_stack = ... */
    }
    else {
        c->c_curthread->t_stack = kmalloc(STACK_SIZE);
        if (c->c_curthread->t_stack == NULL) {
            panic("cpu_create: couldn't allocate stack");
        }
        thread_checkstack_init(c->c_curthread);
    }

    /*
     * If there is no curcpu (or curthread) yet, we are creating
     * the first (boot) cpu. Initialize curcpu and curthread as
     * early as possible so that other code can take locks without
     * exploding.
     */
    if (!CURCPU_EXISTS()) {
        /*
         * Initializing curcpu and curthread is
         * machine-dependent because either of curcpu and
         * curthread might be defined in terms of the other.
         */
        INIT_CURCPU(c, c->c_curthread);

        /*
         * Now make sure both t_cpu and c_curthread are
         * set. This might be partially redundant with
         * INIT_CURCPU depending on how things are defined.
         */
        curthread->t_cpu = curcpu;
        curcpu->c_curthread = curthread;
    }

    result = proc_addthread(kproc, c->c_curthread);
    if (result) {
        panic("cpu_create: proc_addthread:: %s\n", strerror(result));
    }

    cpu_machdep_init(c);

    return c;
}