/*
 * thread.h
 *
 * Copyright (C) 2017 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __MONOLITHIUM_THREAD_H__
#define __MONOLITHIUM_THREAD_H__

#define THREAD_CREATE_FROZEN (1 << 0)

#include "defs.h"
#include "cpu.h"

typedef dword_t (*thread_procedure_t)(void*);

typedef qword_t affinity_t;

typedef enum
{
    THREAD_TID_INFO,
    THREAD_FROZEN_INFO,
    THREAD_CPU_STATE_INFO,
    THREAD_PRIORITY_INFO,
    THREAD_AFFINITY_INFO,
} thread_info_t;

typedef enum
{
    THREAD_PRIORITY_HIGH,
    THREAD_PRIORITY_MID,
    THREAD_PRIORITY_LOW,
    THREAD_PRIORITY_IDLE,

    THREAD_PRIORITY_MAX
} priority_t;

typedef struct
{
    registers_t regs;
    byte_t fpu_state[512];
} thread_state_t;

static inline void init_thread_stack(uintptr_t *stack_pointer, void *parameter)
{
    static const byte_t thread_end_code[] = {
        /* push eax */
        0x50,

        /* push INVALID_HANDLE */
        0x68,
        INVALID_HANDLE & 0xFF,
        (INVALID_HANDLE >> 8) & 0xFF,
        (INVALID_HANDLE >> 16) & 0xFF,
        (INVALID_HANDLE >> 24) & 0xFF,

        /* mov eax, SYSCALL_TERMINATE_THREAD */
        0xB8,
        SYSCALL_TERMINATE_THREAD & 0xFF,
        (SYSCALL_TERMINATE_THREAD >> 8) & 0xFF,
        (SYSCALL_TERMINATE_THREAD >> 16) & 0xFF,
        SYSCALL_TERMINATE_THREAD >> 24,

        /* mov edx, esp */
        0x8B, 0xD4,

        /* int SYSCALL_INTERRUPT */
        0xCD, SYSCALL_INTERRUPT
    };

    *stack_pointer -= (sizeof(thread_end_code) + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1);
    __builtin_memcpy((void*)*stack_pointer, thread_end_code, sizeof(thread_end_code));

    uintptr_t end_code_addr = *stack_pointer;
    push_to_stack(stack_pointer, (uintptr_t)parameter);
    push_to_stack(stack_pointer, end_code_addr);
}

sysret_t syscall_open_thread(dword_t tid, handle_t *handle);
sysret_t syscall_create_thread(handle_t process, thread_state_t *initial_state, dword_t flags, priority_t priority, handle_t *new_thread);
sysret_t syscall_terminate_thread(handle_t thread, dword_t return_value);
sysret_t syscall_freeze_thread(handle_t handle);
sysret_t syscall_thaw_thread(handle_t handle);
sysret_t syscall_sleep(qword_t milliseconds);
sysret_t syscall_yield_quantum(void);
sysret_t syscall_query_thread(handle_t handle, thread_info_t info_type, void *buffer, size_t size);
sysret_t syscall_set_thread(handle_t handle, thread_info_t info_type, const void *buffer, size_t size);

#endif
