/*
 * Copyright (c) 2007, 2008 University of Tsukuba
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. Neither the name of the University of Tsukuba nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * Copyright (c) 2010-2012 Yuichi Watanabe
 */

#include <common/list.h>
#include <core/assert.h>
#include <core/cpu.h>
#include <core/initfunc.h>
#include <core/linkage.h>
#include <core/printf.h>
#include <core/spinlock.h>
#include <core/string.h>
#include "mm.h"
#include "panic.h"
#include "pcpu.h"
#include "thread.h"
#include "thread_switch.h"

#define MAXNUM_OF_THREADS	256
#define CPU_ID_ANY		MAX_CPU_ID + 1

extern ulong volatile syscallstack asm ("%gs:gs_syscallstack");

enum thread_state {
	THREAD_EXIT,
	THREAD_RUN,
	THREAD_WILL_STOP,
	THREAD_STOP,
};

struct thread_context {
	ulong r12, r13, r14_edi, r15_esi, rbx, rbp, rip;
};

struct thread_data {
	LIST1_DEFINE (struct thread_data);
	struct thread_context *context;
	tid_t tid;
	enum thread_state state;
	cpu_id_t cpu_id;
	bool boot;
	void *stack;
	int pid;
	ulong syscallstack;
	phys_t process_switch;
	bool process_switch_enable;
};

static struct thread_data td[MAXNUM_OF_THREADS];
static LIST1_DEFINE_HEAD_INIT(struct thread_data, td_free);
static LIST1_DEFINE_HEAD_INIT(struct thread_data, td_runnable);
static spinlock_t thread_lock;

static void
thread_data_init (struct thread_data *d, struct thread_context *c, void *stack,
		  cpu_id_t cpu_id)
{
	d->context = c;
	d->cpu_id = cpu_id;
	d->boot = false;
	d->stack = stack;
	d->pid = 0;
	d->syscallstack = 0;
	d->process_switch_enable = false;
	d->state = THREAD_RUN;
}

static void
thread_data_save (struct thread_data *d)
{
	d->stack = currentcpu->stackaddr;
	d->pid = currentcpu->pid;
	d->syscallstack = syscallstack;
}

static void
thread_data_load (struct thread_data *d)
{
	currentcpu->stackaddr = d->stack;
	currentcpu->pid = d->pid;
	syscallstack = d->syscallstack;
	if (d->process_switch_enable)
		mm_process_switch (d->process_switch);
}

tid_t
thread_gettid (void)
{
	return currentcpu->tid;
}

static void
switched (void)
{
	spinlock_unlock (&thread_lock);
}

void
schedule (void)
{
	struct thread_data *d;
	tid_t oldtid, newtid;

	spinlock_lock (&thread_lock);
	LIST1_FOREACH (td_runnable, d) {
		if (d->cpu_id == CPU_ID_ANY ||
		    d->cpu_id == get_cpu_id())
			goto found;
	}
	spinlock_unlock (&thread_lock);
	return;
found:
	LIST1_DEL (td_runnable, d);
	oldtid = currentcpu->tid;
	newtid = d->tid;
	thread_data_save (&td[oldtid]);
	currentcpu->tid = newtid;
	thread_data_load (d);
	switch (td[oldtid].state) {
	case THREAD_EXIT:
		LIST1_ADD (td_free, &td[oldtid]);
		break;
	case THREAD_RUN:
		LIST1_ADD (td_runnable, &td[oldtid]);
		break;
	case THREAD_WILL_STOP:
		td[oldtid].state = THREAD_STOP;
		break;
	case THREAD_STOP:
	default:
		panic ("schedule: bad state tid=%d state=%d",
		       oldtid, td[oldtid].state);
	}
	thread_switch (&td[oldtid].context, d->context, 0);
	switched ();
}

asmlinkage void
thread_start1 (void (*func) (void *), void *arg)
{
	switched ();
	func (arg);
	thread_exit ();
}

tid_t
thread_new_bind(void (*func) (void *), void *arg, int stacksize,
		cpu_id_t cpu_id)
{
	u8 *stack, *q;
	struct thread_data *d;
	struct thread_context c;
	tid_t r;

	stack = alloc (stacksize);
	if (stack == NULL) {
		panic("thread_new: Failed to allocate a stack.");
	}
	q = stack + stacksize;
	c.rbp = 0;
	c.rip = (ulong)thread_start0;
#define PUSH(n) memcpy (q -= sizeof (n), &(n), sizeof (n))
	PUSH (arg);
	PUSH (func);
	PUSH (c);
#undef PUSH

	spinlock_lock (&thread_lock);
	d = LIST1_POP (td_free);
	ASSERT (d);
	thread_data_init (d, (struct thread_context *)q, stack, cpu_id);
	LIST1_ADD (td_runnable, d);
	r = d->tid;
	spinlock_unlock (&thread_lock);
	return r;
}

tid_t
thread_new (void (*func) (void *), void *arg, int stacksize)
{
	return thread_new_bind(func, arg, stacksize, CPU_ID_ANY);
}

static enum thread_state
thread_set_state (tid_t tid, enum thread_state state)
{
	enum thread_state oldstate;

	spinlock_lock (&thread_lock);
	oldstate = td[tid].state;
	td[tid].state = state;
	spinlock_unlock (&thread_lock);
	return oldstate;
}

void
thread_wakeup (tid_t tid)
{
	switch (thread_set_state (tid, THREAD_RUN)) {
	case THREAD_RUN:
		printf ("WARNING: waking up runnable thread tid=%d\n", tid);
		break;
	case THREAD_WILL_STOP:
		break;
	case THREAD_STOP:
		spinlock_lock (&thread_lock);
		LIST1_ADD (td_runnable, &td[tid]);
		spinlock_unlock (&thread_lock);
		break;
	case THREAD_EXIT:
	default:
		panic ("thread_wakeup: bad state tid=%d state=%d",
		       tid, td[tid].state);
	}
}

void
thread_will_stop (void)
{
	switch (thread_set_state (currentcpu->tid, THREAD_WILL_STOP)) {
	case THREAD_RUN:
		break;
	case THREAD_WILL_STOP:
		printf ("WARNING: thread_will_stop called twice tid=%d\n",
			currentcpu->tid);
		break;
	case THREAD_STOP:
	case THREAD_EXIT:
	default:
		panic ("thread_will_stop: bad state tid=%d state=%d",
		       currentcpu->tid, td[currentcpu->tid].state);
	}
}

void
thread_exit (void)
{
	switch (thread_set_state (currentcpu->tid, THREAD_EXIT)) {
	case THREAD_EXIT:
		printf ("WARNING: thread already exited tid=%d\n",
			currentcpu->tid);
		break;
	case THREAD_RUN:
		break;
	case THREAD_WILL_STOP:
		printf ("thread_exit called after thread_will_stop tid=%d\n",
			currentcpu->tid);
		break;
	case THREAD_STOP:
	default:
		panic ("thread_exit: bad state tid=%d state=%d",
		       currentcpu->tid, td[currentcpu->tid].state);
	}
	schedule ();
}

void
thread_set_process_switch (phys_t switchto)
{
	td[currentcpu->tid].process_switch = switchto;
	td[currentcpu->tid].process_switch_enable = true;
}

static void
thread_init_global (void)
{
	int i;

	spinlock_init (&thread_lock);
	for (i = 0; i < MAXNUM_OF_THREADS; i++) {
		td[i].tid = i;
		td[i].state = THREAD_EXIT;
		LIST1_ADD (td_free, &td[i]);
	}
}

static void
thread_init_pcpu (void)
{
	struct thread_data *d;

	spinlock_lock (&thread_lock);
	d = LIST1_POP (td_free);
	ASSERT (d);
	thread_data_init (d, NULL, NULL, get_cpu_id());
	d->boot = true;
	currentcpu->tid = d->tid;
	spinlock_unlock (&thread_lock);
}

INITFUNC ("global3", thread_init_global);
INITFUNC ("pcpu0", thread_init_pcpu);
