1/**
2 * \file
3 * \brief x86-64 interrupt/exception handling
4 */
5
6/*
7 * Copyright (c) 2007, 2008, 2009, 2010, ETH Zurich.
8 * All rights reserved.
9 *
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
13 */
14
15/*-
16 * Copyright (c) 1989, 1990 William F. Jolitz
17 * Copyright (c) 1990 The Regents of the University of California.
18 * All rights reserved.
19 *
20 * This code is derived from software contributed to Berkeley by
21 * William Jolitz.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 *    notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 *    notice, this list of conditions and the following disclaimer in the
30 *    documentation and/or other materials provided with the distribution.
31 * 4. Neither the name of the University nor the names of its contributors
32 *    may be used to endorse or promote products derived from this software
33 *    without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 *
47 *      from: @(#)segments.h    7.1 (Berkeley) 5/9/91
48 * $FreeBSD$
49 */
50
51#ifndef KERNEL_IRQ_H
52#define KERNEL_IRQ_H
53
54/*
55 * AMD64 Segmentation Data Structures and definitions
56 */
57
58/*
59 * Selectors
60 */
61
62#define SEL_RPL_MASK    3       /* requester priv level */
63#define ISPL(s) ((s)&3)         /* what is the priority level of a selector */
64#define SEL_KPL 0               /* kernel priority level */
65#define SEL_UPL 3               /* user priority level */
66#define ISLDT(s)        ((s)&SEL_LDT)   /* is it local or global */
67#define SEL_LDT 4               /* local descriptor table */
68#define IDXSEL(s)       (((s)>>3) & 0x1fff)             /* index of selector */
69#define LSEL(s,r)       (((s)<<3) | SEL_LDT | r)        /* a local selector */
70#define GSEL(s,r)       (((s)<<3) | r)                  /* a global selector */
71
72/**
73 * Gate descriptors (e.g. indirect descriptors, trap, interrupt etc. 128 bit)
74 * Only interrupt and trap gates have gd_ist.
75 */
76struct  gate_descriptor {
77    uint64_t gd_looffset:16;       /* gate offset (lsb) */
78    uint64_t gd_selector:16;       /* gate segment selector */
79    uint64_t gd_ist:3;             /* IST table index */
80    uint64_t gd_xx:5;              /* unused */
81    uint64_t gd_type:5;            /* segment type */
82    uint64_t gd_dpl:2;             /* segment descriptor priority level */
83    uint64_t gd_p:1;               /* segment descriptor present */
84    uint64_t gd_hioffset:48;       /* gate offset (msb) */
85    uint64_t sd_xx1:32;
86} __attribute__((packed));
87
88/* system segments and gate types */
89#define SDT_SYSNULL      0      /* system null */
90#define SDT_SYSLDT       2      /* system 64 bit local descriptor table */
91#define SDT_SYSTSS       9      /* system available 64 bit TSS */
92#define SDT_SYSBSY      11      /* system busy 64 bit TSS */
93#define SDT_SYSCGT      12      /* system 64 bit call gate */
94#define SDT_SYSIGT      14      /* system 64 bit interrupt gate */
95#define SDT_SYSTGT      15      /* system 64 bit trap gate */
96
97/* memory segment types */
98#define SDT_MEMRO       16      /* memory read only */
99#define SDT_MEMROA      17      /* memory read only accessed */
100#define SDT_MEMRW       18      /* memory read write */
101#define SDT_MEMRWA      19      /* memory read write accessed */
102#define SDT_MEMROD      20      /* memory read only expand dwn limit */
103#define SDT_MEMRODA     21      /* memory read only expand dwn limit accessed */
104#define SDT_MEMRWD      22      /* memory read write expand dwn limit */
105#define SDT_MEMRWDA     23      /* memory read write expand dwn limit accessed */
106#define SDT_MEME        24      /* memory execute only */
107#define SDT_MEMEA       25      /* memory execute only accessed */
108#define SDT_MEMER       26      /* memory execute read */
109#define SDT_MEMERA      27      /* memory execute read accessed */
110#define SDT_MEMEC       28      /* memory execute only conforming */
111#define SDT_MEMEAC      29      /* memory execute only accessed conforming */
112#define SDT_MEMERC      30      /* memory execute read conforming */
113#define SDT_MEMERAC     31      /* memory execute read accessed conforming */
114
115/*
116 * Size of IDT table
117 */
118#define NIDT    256             /* 32 reserved, 16 h/w, 0 s/w, linux's 0x80 */
119
120/// Number of (reserved) hardware exceptions
121#define NEXCEPTIONS             32
122
123/// Size of hardware IRQ dispatch table == #NIDT - #NEXCEPTIONS exceptions
124#define NDISPATCH               (NIDT - NEXCEPTIONS)
125
126
127/*
128 * Entries in the Global Descriptor Table (GDT)
129 */
130#define NULL_SEL        0       /**< Null descriptor */
131#define KCODE_SEL       1       /**< Kernel code descriptor */
132#define KSTACK_SEL      2       /**< Shared user/kernel stack descriptor */
133#define USTACK_SEL      3       /**< User stack descriptor */
134#define UCODE_SEL       4       /**< User code descriptor */
135#define TSS_LO_SEL      5       /**< Task State Segment (TSS) -- low 64bit */
136#define TSS_HI_SEL      6       /**< Task State Segment (TSS) -- high 64bit */
137#define LDT_LO_SEL      7       /**< Local descriptor table (LDT) -- low */
138#define LDT_HI_SEL      8       /**< Local descriptor table (LDT) -- high */
139#define NGDT_MEM        9       /**< Number of descriptors */
140
141/**
142 * region descriptors, used to load gdt/idt tables before segments yet exist.
143 */
144struct region_descriptor {
145    uint16_t rd_limit;          /**< segment extent */
146    uint64_t rd_base;           /**< base address  */
147} __attribute__((packed));
148
149struct task_state_segment {
150    uint32_t    reserved;
151    uint64_t    rsp[3];
152    uint64_t    reserved2;
153    uint64_t    ist[7];
154    uint64_t    reserved3;
155    uint16_t    reserved4;
156    uint16_t    iomap_base;
157} __attribute__ ((packed));
158
159void setup_default_idt(void);
160
161errval_t irq_connect(struct capability *dest_cap, capaddr_t endpoint_adr);
162errval_t irq_table_alloc(int *outvec);
163errval_t irq_table_set(unsigned int nidt, capaddr_t endpoint);
164errval_t irq_table_delete(unsigned int nidt);
165struct kcb;
166errval_t irq_table_notify_domains(struct kcb *kcb);
167errval_t irq_table_alloc_dest_cap(uint8_t dcn_vbits, capaddr_t dcn,
168        capaddr_t out_cap_addr);
169
170#endif
171