/*
 * thread.h -  Thread and related operations definition.
 *
 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
 *         Alain Greiner (2016)
 *
 * Copyright (c) UPMC Sorbonne Universites
 *
 * This file is part of ALMOS-MKH.
 *
 * ALMOS-MKH 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; version 2.0 of the License.
 *
 * ALMOS-MKH 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 ALMOS-MKH; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef _THREAD_H_
#define _THREAD_H_

#include <hal_types.h>
#include <shared_syscalls.h>
#include <hal_special.h>
#include <xlist.h>
#include <list.h>
#include <hal_context.h>
#include <spinlock.h>
#include <core.h>
#include <chdev.h>
#include <cluster.h>
#include <process.h>
#include <dev_ioc.h>
#include <dev_nic.h>
#include <dev_txt.h>
#include <dev_mmc.h>
#include <dev_dma.h>

/***************************************************************************************
 * These macros are used to compose or decompose the global thread identifier (TRDID)
 * to or from cluster identifier / local thread index (CXY , LTID)
 **************************************************************************************/

#define LTID_FROM_TRDID( trdid )   (ltid_t)(trdid & 0x0000FFFF)
#define CXY_FROM_TRDID( trdid )    (cxy_t)(trdid >> 16)
#define TRDID( cxy , ltid )        (trdid_t)((cxy << 16) | ltid )

/***************************************************************************************
 * This enum defines the thread types.
 **************************************************************************************/

typedef enum
{
	THREAD_USER    = 0,          /*! user thread (pthread)                            */
	THREAD_RPC     = 1,          /*! kernel thread executing pending RPCs             */
	THREAD_DEV     = 2,          /*! kernel thread executing I/O device commands      */
	THREAD_IDLE    = 3,          /*! kernel idle thread                               */
}
thread_type_t;

/***************************************************************************************
 * This defines the thread flags bit-vector.
 **************************************************************************************/

#define THREAD_FLAG_DETACHED     0x0001  /*! This thread is detached from parent      */
#define THREAD_FLAG_JOIN_DONE    0x0002  /*! Parent thread made a join                */
#define THREAD_FLAG_SCHED        0x0004  /*! Scheduling required for this thread      */
#define THREAD_FLAG_REQ_ACK      0x0008  /*! Acknowledge required from scheduler      */
#define THREAD_FLAG_REQ_DELETE   0x0010  /*! Destruction required by scheduler        */

/***************************************************************************************
 * This defines the masks associated to the blocking causes.
 **************************************************************************************/

#define THREAD_BLOCKED_GLOBAL    0x0001  /*! thread deactivated / wait activation     */
#define THREAD_BLOCKED_IO        0x0002  /*! thread wait IO operation completion      */
#define THREAD_BLOCKED_MAPPER    0x0004  /*! thread wait mapper                       */
#define THREAD_BLOCKED_EXIT      0x0008  /*! thread blocked in join / wait exit       */
#define THREAD_BLOCKED_JOIN      0x0010  /*! thread blocked in exit / wait join       */
#define THREAD_BLOCKED_SEM       0x0020  /*! thread wait semaphore                    */
#define THREAD_BLOCKED_PAGE      0x0040  /*! thread wait page access                  */
#define THREAD_BLOCKED_USERSYNC  0x0100  /*! thread wait (cond/mutex/barrier)         */
#define THREAD_BLOCKED_RPC       0x0200  /*! thread wait RPC completion               */
#define THREAD_BLOCKED_DEV_ISR   0x0400  /*! thread DEV wait ISR                      */

/***************************************************************************************
 * This structure defines thread instrumentation informations.
 **************************************************************************************/

typedef struct thread_info_s
{
	uint32_t              pgfault_nr;    /*! cumulated number of page fault           */
	uint32_t              sched_nr;      /*! TODO ???  [AG]                           */
	uint32_t              u_err_nr;      /*! TODO ???  [AG]                           */
	uint32_t              m_err_nr;      /*! TODO ???  [AG]                           */
	uint32_t              tm_tmp;        /*! temp date to compute execution duration  */
	uint32_t              tm_exec;       /*! TODO ???  [AG]                           */
	uint32_t              tm_create;     /*! date of the creation                     */
	uint32_t              tm_born;       /*! date of the thread loading               */
	uint32_t              tm_dead;       /*! date of the death                        */
	cycle_t               tm_sleep;      /*! TODO ???  [AG]                           */
	cycle_t               tm_wait;       /*! TODO ???  [AG]                           */
	cycle_t               tm_usr;        /*! user execution duration                  */
	cycle_t               tm_sys;        /*! system execution duration                */
}
thread_info_t;

/***************************************************************************************
 * This structure defines a thread descriptor.
 * It is used for both the user threads and the kernel threads.
 * In a process, a user thread is identified by a unique TRDID (thread identifier),
 * that is returned by the kernel to the user:
 * - The TRDID 16 LSB bits contain the LTID (Local Thread Index).
 * - The TRDID 16 MSB bits contain the CXY of cluster containing the thread.
 * - The LTID is used to index the th_tbl[] array in the local process descriptor.
 _* This TRDID is computed by the process_register_thread() function, when the user
 * thread is registered in the local copy of the process descriptor.
 *
 * WARNING : Don't modify the first 3 fields order, as this order is used by the
 * hal_kentry assembly code for the TSAR architecture.
 **************************************************************************************/

#define THREAD_SIGNATURE    0xDEADBEEF

typedef struct thread_s
{
	void              * cpu_context;     /*! pointer on CPU context switch            */
	void              * fpu_context;     /*! pointer on FPU context switch            */
    void              * uzone;           /*! pointer on uzone for hal_kentry          */

	intptr_t            k_stack_base;    /*! kernel stack base address                */
	uint32_t            k_stack_size;    /*! kernel stack size (bytes)                */

	uint32_t            trdid;           /*! thread index (cxy.ltid)                  */
	thread_type_t       type;            /*! thread type                              */
	uint32_t            quantum;         /*! number of clock ticks given to thread    */
	uint32_t            ticks_nr;        /*! number of ticks used                     */
	uint32_t            time_last_check; /*! last cpu_time_stamp                      */
	core_t            * core;            /*! pointer to the owner core                */
	process_t         * process;         /*! pointer on local process descriptor      */
    xptr_t              parent;          /*! extended pointer on parent thread        */

	uint32_t            local_locks;	 /*! number of local locks owned by thread    */
	uint32_t            remote_locks;	 /*! number of remote locks owned by thread   */

    remote_spinlock_t * join_lock;       /*! lock protecting the join/exit            */
    void              * join_value;      /*! exit_value used in case of join          */
    xptr_t              join_xp;         /*! extended pointer on joining thread       */

    uint32_t          * ack_rsp_count;   /*! pointer on acknowledge response counter  */

	intptr_t            u_stack_base;    /*! user stack base address                  */
	uint32_t            u_stack_size;    /*! user stack size (bytes)                  */

    void              * entry_func;      /*! pointer on entry function                */
    void              * entry_args;      /*! pointer on entry function arguments      */

    uint32_t            flags;           /*! bit vector of flags                      */
    uint32_t            blocked;         /*! bit vector of blocking causes            */

	error_t             errno;           /*! errno value set by last system call      */
    uint32_t            utls;            /*! user thread local storage                */

    bool_t              fork_user;       /*! user defined placement for next fork()   */
    cxy_t               fork_cxy;        /*! target cluster  for next fork()          */

	xlist_entry_t       children_root;   /*! root of list of attached children        */
    uint32_t            children_nr;     /*! number of attached children threads      */
    remote_spinlock_t * children_lock;   /*! lock protecting the children list        */

    xlist_entry_t       brothers_list;   /*! list of attached threads to same parent  */

	list_entry_t        sched_list;      /*! member of threads attached to same core  */

    chdev_t           * chdev;           /*! chdev pointer (for a DEV thread only)    */

    reg_t               save_sr;         /*! used by sched_yield() function           */

    ioc_command_t       ioc_cmd;         /*! IOC device generic command               */
    txt_command_t       txt_cmd;         /*! TXT device generic command               */
    nic_command_t       nic_cmd;         /*! NIC device generic command               */
    mmc_command_t       mmc_cmd;         /*! MMC device generic command               */
    dma_command_t       dma_cmd;         /*! DMA device generic command               */

	cxy_t               rpc_client_cxy;  /*! client cluster index (for a RPC thread)  */

    xlist_entry_t       wait_list;       /*! member of threads blocked on same cond   */

#if CONFIG_LOCKS_DEBUG
    list_entry_t        locks_root;      /*! root of list of locks taken              */
    xlist_entry_t       xlocks_root;     /*! root of xlist of remote locks taken      */
#endif

	thread_info_t       info;            /*! embedded thread_info_t                   */

	uint32_t            signature;       /*! for kernel stack overflow detection      */
}
thread_t;

/***************************************************************************************
 * This macro returns a pointer on the calling thread from the core hardware register.
 **************************************************************************************/

#define CURRENT_THREAD  (hal_get_current_thread())

/***************************************************************************************
 * This function returns a printable string for a thread type.
 ***************************************************************************************
 * @ type    : thread type.
 * returns pointer on string.
 **************************************************************************************/
char * thread_type_str( uint32_t type );

/***************************************************************************************
 * This function is used by the pthread_create() system call to create a "new" thread
 * in an existing process. It allocates memory for an user thread descriptor in the
 * local cluster, and initializes it from information contained in the arguments.
 * The CPU context is initialized from scratch. If required by the <attr> argument,
 * the new thread is attached to the core specified in <attr>.
 * It is registered in the local process descriptor specified by the <pid> argument.
 * The thread descriptor pointer is returned to allow the parent thread to register it
 * in its children list.
 * The THREAD_BLOCKED_GLOBAL bit is set => the thread must be activated to start.
 ***************************************************************************************
 * @ pid          : process identifier.
 * @ start_func   : pointer on entry function.
 * @ start_args   : pointer on function argument (can be NULL).
 * @ attr         : pointer on pthread attributes descriptor.
 * @ new_thread   : [out] address of buffer for new thread descriptor pointer.
 * @ returns 0 if success / returns ENOMEM if error.
 **************************************************************************************/
error_t thread_user_create( pid_t             pid,
                            void            * start_func,
                            void            * start_arg,
                            pthread_attr_t  * attr,
                            thread_t       ** new_thread );

/***************************************************************************************
 * This function is used by the sys_fork() system call to create the "child" thread
 * in the local cluster. It allocates memory for a thread descriptor, and initializes
 * it from the "parent" thread descriptor defined by the <parent_thread_xp> argument.
 * The new thread is attached to the core that has the lowest load in local cluster.
 * It is registered in the "child" process defined by the <child_process> argument.
 * This new thread inherits its user stack from the parent thread, as it uses the
 * Copy-On-Write mechanism to get a private stack when required.
 * The content of the parent kernel stack is copied into the child kernel stack, as 
 * the Copy-On-Write mechanism cannot be used for kernel segments (because kernel 
 * uses physical addressing on some architectures).
 * The CPU and FPU execution contexts are created and linked to the new thread.
 * but the actual context copy is NOT done, and must be done by by the sys_fork(). 
 * The THREAD_BLOCKED_GLOBAL bit is set => the thread must be activated to start.
 ***************************************************************************************
 * @ parent_thread_xp  : extended pointer on parent thread descriptor.
 * @ child_process     : local pointer on child process descriptor.
 * @ child_thread      : [out] address of buffer for child thread descriptor pointer.
 * @ returns 0 if success / returns -1 if error.
 **************************************************************************************/
error_t thread_user_fork( xptr_t      parent_thread_xp,
                          process_t * child_process,
                          thread_t ** child_thread );

/***************************************************************************************
 * This function allocates memory for a kernel thread descriptor in the local cluster,
 * and initializes it from arguments values. 
 * It is called by kernel_init() to statically create all DEV server threads
 * It is also called to dynamically create RPC threads when required.
 * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.
 ***************************************************************************************
 * @ new_thread   : address of buffer for new thread pointer.
 * @ type         : kernel thread type.
 * @ func         : pointer on function.
 * @ args         : function arguments.
 * @ core_lid     : local core index.
 * @ returns 0 if success / returns ENOMEM if error
 **************************************************************************************/
error_t thread_kernel_create( thread_t     ** new_thread,
                              thread_type_t   type,
                              void          * func,
                              void          * args,
                              lid_t           core_lid );

/***************************************************************************************
 * This function initializes an existing thread descriptor from arguments values.
 * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.
 * It is called by the kernel_init() function to initialize the IDLE thread.
 ***************************************************************************************
 * @ thread   : pointer on existing thread descriptor.
 * @ type     : kernel thread type.
 * @ func     : pointer on function.
 * @ args     : function arguments.
 * @ core_lid : local core index.
 * @ returns 0 if success / returns EINVAL if error
 **************************************************************************************/
error_t thread_kernel_init( thread_t      * thread,
                            thread_type_t   type,
                            void          * func,
                            void          * args,
                            lid_t           core_lid );

/***************************************************************************************
 * This function releases the physical memory allocated for a thread in a given cluster.
 * This include the thread descriptor itself, the associated CPU and FPU context, and
 * the physical memory allocated for an user thread local stack.
 ***************************************************************************************
 * @ thread  : pointer on the thread descriptor to release.
 **************************************************************************************/
void thread_destroy( thread_t * thread );

/***************************************************************************************
 * This function defines the code of the thread executed by all cores after kernel_init,
 * or when no other thread is runnable for a given core.
 *
 * TODO: In the TSAR architecture, it enters an infinite loop, in wich it forces
 * the core in sleep (low-power) mode. Any IRQ will force the core to exit this sleep
 * mode, but no ISR is executed.
 * TODO: We must analyse if we have the same behaviour for I86 architectures...
 **************************************************************************************/
void thread_idle_func();

/***************************************************************************************
 * This function registers a child thread in the global list of attached
 * children threads of a parent thread.
 * It does NOT take a lock, as this function is always called by the parent thread.
 ***************************************************************************************
 * @ parent_xp : extended pointer on the parent thread descriptor.
 * @ child_xp  : extended pointer on the child thread descriptor.
 **************************************************************************************/
void thread_child_parent_link( xptr_t  parent_xp,
                               xptr_t  child_xp );

/***************************************************************************************
 * This function removes an user thread from the parent thread global list
 * of attached children threads.
 ***************************************************************************************
 * @ parent_xp : extended pointer on the parent thread descriptor.
 * @ child_xp  : extended pointer on the child thread descriptor.
 **************************************************************************************/
void thread_child_parent_unlink( xptr_t parent_xp,
                                 xptr_t child_xp );

/***************************************************************************************
 * This function is used by a "blocker" thread running in the same cluster as a "target"
 * thread to request the scheduler of the target thread to acknowledge that the target
 * thread is blocked and not running, at the next context switch.
 * This function executes atomically the following actions :
 * - it set the request_pending boolean in the target scheduler descriptor.
 * - it set the REQ_ACK flag in the "flags" field of the target thread descriptor.
 * - It registers the responses counter pointer in the target thread descriptor.
 * The request_pending flag is handled as a set/reset flip-flop by the "blocker" thread
 * and by the "target" scheduler. 
 ***************************************************************************************
 * @ target        : local pointer on target thread.
 * @ ack_rsp_count : local pointer on responses counter.
 **************************************************************************************/
void thread_set_req_ack( thread_t * target,
                         uint32_t * ack_rsp_count );

/***************************************************************************************
 * This function is used by the sched_handle_signal() function executed by the 
 * scheduler of a "target" thread to reset a "blocked not running" acknowledge request
 * in both the target thread descriptor, and in the target  thread scheduler.
 ***************************************************************************************
 * @ target    : local pointer on target thread.
 **************************************************************************************/
void thread_reset_req_ack( thread_t * target );

/***************************************************************************************
 * This function checks if the calling thread can deschedule.
 ***************************************************************************************
 * @ returns true if no locks taken.
 **************************************************************************************/
inline bool_t thread_can_yield();

/***************************************************************************************
 * This function implements the delayed descheduling mechanism : It is called  by 
 * all lock release functions, and calls the sched_yield() function when all locks 
 * have beeen released and the calling thread THREAD_FLAG_SCHED flag is set. 
 **************************************************************************************/
void thread_check_sched();

/***************************************************************************************
 * This function is called to handle the "pthread_cancel" system call.
 * It allows a killer thread to kill one single target thread.
 * The killer thread must be running in the same cluster as the target thread.
 * If not, the client thread must use the RPC_THREAD_KILL.
 * - When the killer thread is running on the same core as the target thread,
 *   this function simply set the BLOCKED_ GLOBAL bit and the REQ_DELETE flag
 *   in the target thread descriptor and return.
 * - When the killer thread is running on a different core than the target thread,
 *   the killer set the BLOCKED_GLOBAL bit and the REQ_ACK flag in target  thread,
 *   to ask the scheduler to confirm that the target is blocked and not running.
 *   Then, it set the REQ_DELETE flag in the target thread and return.
 * In both cases, the actual target thread destruction is done by the scheduler
 * at the next scheduling point.
 ***************************************************************************************
 * @ thread   : local pointer on the target thread.
 **************************************************************************************/
void thread_kill( thread_t * thread );

/***************************************************************************************
 * This function registers a blocking cause in the target thread "blocked" bit vector.
 * Warning : this function does not deschedule the calling thread, and the descheduling
 * must be explicitely forced by a sched_yield().
 ***************************************************************************************
 * @ thread   : local pointer on target thread descriptor.
 * @ cause    : mask defining the cause (one hot).
 **************************************************************************************/
void thread_block( thread_t * thread,
                   uint32_t   cause );

/***************************************************************************************
 * This function resets the bit identified by the cause argument in the "blocked"
 * bit vector of a remote thread descriptor, using an atomic access.
 * We need an extended pointer, because the client thread of an I/O operation on a
 * given device is not in the same cluster as the associated device descriptor.
 * Warning : this function does not reschedule the remote thread.
 * The scheduling can be forced by sending an IPI to the core running the remote thread.
 ***************************************************************************************
 * @ thread   : extended pointer on the remote thread.
 * @ cause    : mask defining the cause (one hot).
 * @ return non zero if the bit-vector was actually modified / return 0 otherwise
 **************************************************************************************/
uint32_t thread_unblock( xptr_t   thread,
                         uint32_t cause );

/***************************************************************************************
 * This function updates the calling thread user_time counter, and resets the thread
 * cycles counter.
 * TODO This function is not implemented.
 ***************************************************************************************
 * @ thread   : local pointer on target thread.
 **************************************************************************************/
void thread_user_time_update( thread_t * thread );

/**************************************************************************************n
 * This function updates the calling thread kernel_time counter, and resets the thread
 * cycles counter.
 * TODO This function is not implemented.
 ***************************************************************************************
 * @ thread   : local pointer on target thread.
 **************************************************************************************/
void thread_kernel_time_update( thread_t * thread );

/***************************************************************************************
 * This function handles all pending signals for the thread identified by the <thread>
 * argument. It is called each time the core exits the kernel, after handling an
 * interrupt, exception or syscall.
 * TODO This function is not implemented.
 ***************************************************************************************
 * @ thread   : local pointer on target thread.
 **************************************************************************************/
void thread_signals_handle( thread_t * thread );

/***************************************************************************************
 * This function returns the extended pointer on a thread descriptor identified
 * by its thread identifier, and process identifier.
 * It can be called by any thread running in any cluster.
 ***************************************************************************************
 * @ pid     : process identifier.
 * @ trdid   : thread identifier.
 * @ return the extended pointer if thread found / return XPTR_NULL if not found.
 **************************************************************************************/
xptr_t thread_get_xptr( pid_t    pid,
                        trdid_t  trdid );


#endif	/* _THREAD_H_ */
