
/*
 *	linux/include/asm/hw_irq.h
 *
 *	(C) 1992, 1993 Linus Torvalds, (C) 1997 Ingo Molnar
 *
 *	moved some of the old arch/i386/kernel/irq.h to here. VY
 *
 *	IRQ/IPI changes taken from work by Thomas Radke
 *	<tomsoft@informatik.tu-chemnitz.de>
 */

//#include <linux/config.h>
#include <arch/ptrace.h>
#include <arch/segment.h>
#include <arch/mprot.h>
#include <rtl_conf.h>

//#include <asm/irq.h>
/*
 * IDT vectors usable for external interrupt sources start
 * at 0x20:
 */
#define FIRST_EXTERNAL_VECTOR	0x20

#define SYSCALL_VECTOR		0x80

/*
 * Vectors 0x20-0x2f are used for ISA interrupts.
 */

/*
 * Special IRQ vectors used by the SMP architecture, 0xf0-0xff
 *
 *  some of the following vectors are 'rare', they are merged
 *  into a single vector (CALL_FUNCTION_VECTOR) to save vector space.
 *  TLB, reschedule and local APIC vectors are performance-critical.
 *
 *  Vectors 0xf0-0xfa are free (reserved for future Linux use).
 */
#define SPURIOUS_APIC_VECTOR	0xff
#define ERROR_APIC_VECTOR	0xfe
#define INVALIDATE_TLB_VECTOR	0xfd
#define RESCHEDULE_VECTOR	0xfc
#define CALL_FUNCTION_VECTOR	0xfb

/*
 * Local APIC timer IRQ vector is on a different priority level,
 * to work around the 'lost local interrupt if more than 2 IRQ
 * sources per level' errata.
 */
#define LOCAL_TIMER_VECTOR	0xef

/*
 * First APIC vector available to drivers: (vectors 0x30-0xee)
 * we start at 0x31 to spread out vectors evenly between priority
 * levels. (0x80 is the syscall vector)
 */
#define FIRST_DEVICE_VECTOR	0x31
#define FIRST_SYSTEM_VECTOR	0xef


extern char _stext, _etext;

#define IO_APIC_IRQ(x) (((x) >= 16) || ((1<<(x)) & io_apic_irqs))

#define __STR(x) #x
#define STR(x) __STR(x)

#define SAVE_ALL \
	"cld\n\t" \
	"pushl %es\n\t" \
	"pushl %ds\n\t" \
	"pushl %eax\n\t" \
	"pushl %ebp\n\t" \
	"pushl %edi\n\t" \
	"pushl %esi\n\t" \
	"pushl %edx\n\t" \
	"pushl %ecx\n\t" \
	"pushl %ebx\n\t" \
	"movl $0x18,%edx\n\t" \
	"movl %edx,%ds\n\t" \
	"movl %edx,%es\n\t"

#define RESTORE_ALL  	\
	"popl %ebx;   \t\n"	\
	"popl %ecx;   \t\n"	\
	"popl %edx;   \t\n"	\
	"popl %esi;   \t\n"	\
	"popl %edi;   \t\n"	\
	"popl %ebp;   \t\n"	\
	"popl %eax;   \t\n"	\
	"popl %ds;    \t\n"	\
	"popl %es;    \t\n"	\
	"addl $4,%esp;\t\n"	\
	"iret\t\n"		

	
#define IRQ_NAME2(nr) nr##_interrupt(void)
#define IRQ_NAME(nr) IRQ_NAME2(IRQ##nr)

#define GET_CURRENT \
	"movl %esp, %ebx\n\t" \
	"andl $-8192, %ebx\n\t"

/*
 *	SMP has a few special interrupts for IPI messages
 */

	/* there is a second layer of macro just to get the symbolic
	   name for the vector evaluated. This change is for RTLinux */
#define BUILD_SMP_INTERRUPT(x,v) \
asmlinkage void x(void); \
	__asm__( \
"\n"__ALIGN_STR"\n" \
SYMBOL_NAME_STR(x) ":\n\t" \
	"pushl $5\n\t" \
	SAVE_ALL \
	"pushl $ret_from_intr\n\t" \
	"jmp "SYMBOL_NAME_STR(do_IRQ)"\n\t" \
	"jmp ret_from_intr\n");

#define BUILD_SMP_TIMER_INTERRUPT(x,v)  \
asmlinkage void x(struct pt_regs * regs); \
__asm__( \
"\n"__ALIGN_STR"\n" \
SYMBOL_NAME_STR(x) ":\n\t" \
	"pushl $5\n\t" \
	SAVE_ALL \
	"movl %esp,%eax\n\t" \
	"pushl %eax\n\t" \
	"call "SYMBOL_NAME_STR(do_IRQ)"\n\t" \
	"addl $4,%esp\n\t" \
	"jmp ret_from_intr\n");

#define BUILD_COMMON_IRQ() \
__asm__( \
	"\n" __ALIGN_STR"\n" \
	"common_interrupt:\n\t" \
	SAVE_ALL \
	"call "SYMBOL_NAME_STR(do_IRQ)"\n\t" \
	RESTORE_ALL \
	"iret \n\t");

#define BUILD_STEP_INT() \
asmlinkage void stepbystep_interrupt(void); \
__asm__( \
	"\n" __ALIGN_STR"\n" \
	"stepbystep_interrupt:\n\t" \
	"pushl %eax\n\t" \
	SAVE_ALL \
	"rdtsc   \n\t" \
	"movl %eax,(breakpoint_end_time)    \n\t" \
	"movl %edx,(breakpoint_end_time+4)  \n\t" \
	"call "SYMBOL_NAME_STR(StepbyStep_Handler)"\n\t" \
	"rdtsc   \n\t" \
	"movl %eax,(breakpoint_start_time)    \n\t" \
	"movl %edx,(breakpoint_start_time+4)  \n\t" \
	RESTORE_ALL \
	"iret \n\t");

#define BUILD_BREAKPOINT_INT() \
asmlinkage void breakpoint_interrupt(void); \
__asm__( \
	"\n" __ALIGN_STR"\n" \
	"breakpoint_interrupt:\n\t" \
	"pushl %eax\n\t" \
	SAVE_ALL \
	"rdtsc   \n\t" \
	"movl %eax,(breakpoint_end_time)    \n\t" \
	"movl %edx,(breakpoint_end_time+4)  \n\t" \
	"call "SYMBOL_NAME_STR(Breakpoint_Handler)"\n\t" \
	"rdtsc   \n\t" \
	"movl %eax,(breakpoint_start_time)    \n\t" \
	"movl %edx,(breakpoint_start_time+4)  \n\t" \
	RESTORE_ALL \
	"iret \n\t");




#define BUILD_RET_FROM_INTR() \
__asm__( \
	"\n" __ALIGN_STR"\n" \
	"ret_from_intr:\n" \
	"          movb $0x20,%al\n\t" \
        "          outb %al,$0x20\n\t" \
	RESTORE_ALL \
	"               \n");

/* 
 * subtle. orig_eax is used by the signal code to distinct between
 * system calls and interrupted 'random user-space'. Thus we have
 * to put a negative value into orig_eax here. (the problem is that
 * both system calls and IRQs want to have small integer numbers in
 * orig_eax, and the syscall code has won the optimization conflict ;)
 *
 * Subtle as a pigs ear.  VY
 */
#define DEF_DEFAULT_IRQ() \
asmlinkage void default_interrupt(void); \
__asm__( \
          "\n"__ALIGN_STR"\n" \
        "default_interrupt:\n\t" \
        "iret              \n\t"); 

#define BUILD_INT(nr) \
asmlinkage void IRQ##nr##_int_int(void); \
__asm__( \
          "\n"__ALIGN_STR"\n" \
SYMBOL_NAME_STR(IRQ) #nr "_int_int: \n\t" \
	SAVE_ALL \
	" movl $0x61,%eax    \n\t" \
	" addl $"#nr",%eax  \n\t" \
	" movl $0x10,%ecx    \n\t" \
	" movl $0xb8000,%edi \n\t" \
	" rep stosb          \n\t" \
	RESTORE_ALL \
	" int $0x3           \n\t" \
	" hlt                \n\t" \
        "                    \n\t" \
        "iret                \n\t"); 


#define BUILD_IRQ(nr) \
asmlinkage void IRQ_NAME(nr); \
__asm__( \
          "\n"__ALIGN_STR"\n" \
SYMBOL_NAME_STR(IRQ) #nr "_interrupt:\n\t" \
        " pushl $"#nr"-256\n\t"    \
        " jmp common_interrupt \n\t");


// External Declaration
//
extern void set_intr_gate(unsigned int n, void *addr);
extern void set_trap_gate(unsigned int n, void *addr);

