/**
 * @file   drCommon.h
 * @brief  Contains common definitions and data structures
 *
 * Copyright (C) 2012 Giesecke & Devrient GmbH
 */

#ifndef __DRDRMCOMMON_H__
#define __DRDRMCOMMON_H__

#include "DrApi/DrApiCommon.h"
#include "DrApi/DrApiError.h"

/* Priority definitions */
#define EXCH_PRIORITY		MAX_PRIORITY
#define IPCH_PRIORITY		(MAX_PRIORITY - 1)


/* Driver internal error codes */
#define DR_E_OK				0  /* Success */
#define DR_E_CRYPTO_INVALID_PARAMS	1  /* Invalid parameter for AES operation */
#define DR_E_CRYPTO_INTERNAL		2  /* Internal error in AES driver */
#define DR_E_MAP			3  /* Driver mapping error */
#define DR_E_SECMEM_INVALID		4  /* Invalid Secure memory address */
#define DR_E_SECMEM_NOT_ENABLED		5  /* Secure memory not enabled */
#define DR_E_SECMEM_NOT_DISABLED	6  /* Secure memory not disabled */
#define DR_E_SECMEM_NOT_READY		7  /* Secure memory not ready */


/* Kernel exceptions */
#define TRAP_UNKNOWN		(0)    /* unknown exception. */
#define TRAP_SYSCALL		(1)    /* invalid syscall number. */
#define TRAP_SEGMENTATION	(2)    /* illegal memory access. */
#define TRAP_ALIGNMENT		(3)    /* misaligned memory access. */
#define TRAP_UNDEF_INSTR	(4)    /* undefined instruction. */
#define TRAP_BREAKPOINT		(5)    /* breakpoint exception. */
#define TRAP_ARITHMETIC		(6)    /* arithmetic exception. */
#define TRAP_INSTR_FETCH	(7)    /* instruction fetch failure. */
#define TRAP_INTERRUPT		(16)    /* interrupt. */
#define TRAP_TIMEFAULT		(17)    /* timefault exception. */
#define TRAP_TASK_TERMINATE	(18)    /* child task termination. */
#define TRAP_TASK_ACTIVATE	(19)    /* task activation */
#define TRAP_TASK_START		(20)    /* task start */



/* Driver version (used by the IPC handler) */
#define DRIVER_VERSION		1

#define UUID_LENGTH 16

/* Thread numbers */
#define DRIVER_THREAD_NO_EXCH	1
#define DRIVER_THREAD_NO_IPCH	2

#define THREADID_THREADNO_SHIFT		16
#define THREADID_TASKID_MASK		((1 << THREADID_THREADNO_SHIFT) - 1)
#define THREADID_THREADNO_MASK		(~THREADID_TASKID_MASK)
#define GET_THREADNO(threadid)		(threadid >> THREADID_THREADNO_SHIFT)


/**
 * Maximum allowed length is 512KB. Some of the available pages
 * are used for mapping various physical memory locations
 * e.g. SPUM, etc..
 */
#define DR_MAX_INPUT_LENGTH	0x80000


/* Macro and relevat definitions for stack */
#define STACK_DBG_INIT_VALUE	0xCC
#define fillStack(_name_, _val_) \
	memset(_STACK_ARRAY_VAR(_name_), _val_, getStackSize(_name_))
#define clearStack(_name_)	fillStack(_name_, STACK_DBG_INIT_VALUE)
#define TASKID_GET_THREADID(taskid, threadno)	((threadid_t)((taskid) | \
					((threadno) << THREADID_THREADNO_SHIFT)))
#define getStackTop(_name_)	GET_STACK_TOP(_name_)

/* Thread specific definitions */
/* returns true if thread id is NILTHREAD */
#define threadid_is_null(threadid)	(NILTHREAD == threadid)

#endif /* __DRDRMCOMMON_H__ */

