Contiki 3.x
cpu.c
1 /*
2  * Copyright (C) 2015, Intel Corporation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "cpu.h"
32 #include "gdt.h"
33 #include "helpers.h"
34 #include "idt.h"
35 #include "interrupt.h"
36 #include "irq.h"
37 #include "stacks.h"
38 
39 static void
40 double_fault_handler(struct interrupt_context context)
41 {
42  halt();
43 }
44 /*---------------------------------------------------------------------------*/
45 /* The OS has switched to its own segment descriptors. When multi-segment
46  * protection domain support is enabled, this routine runs with the
47  * necessary address translations configured to invoke other routines that
48  * require those translations to be in place. However, the protection domain
49  * support, if enabled, has not yet been fully activated.
50  */
51 static void
52 boot_stage1(void)
53 {
54  idt_init();
55 
56  /* Set an interrupt handler for Double Fault exception. This way, we avoid
57  * the system to triple fault, leaving no trace about what happened.
58  */
59  SET_EXCEPTION_HANDLER(8, 1, double_fault_handler);
60 
61  /* Initialize protection domain support, if enabled */
62  prot_domains_init();
63 
64  prot_domains_leave_boot_stage1();
65 }
66 /*---------------------------------------------------------------------------*/
67 int main(void);
68 /* This routine runs with the initial, flat address space, even if protection
69  * domain support is enabled. The goal behind the design of this routine is to
70  * keep it as short as possible, since it is unable to directly reference data
71  * and invoke functions that are intended to be accessible later after the
72  * system has booted when a multi-segment protection domain model is in use.
73  */
74 void
75 cpu_boot_stage0(void)
76 {
77  /* Reserve three stack slots for return addresses */
78  uintptr_t top_of_stack = STACKS_INIT_TOP;
79 
80 #if X86_CONF_PROT_DOMAINS != X86_CONF_PROT_DOMAINS__NONE
81  uintptr_t *top_of_stack_ptr =
82  (uintptr_t *)DATA_OFF_TO_PHYS_ADDR(top_of_stack);
83 
84  top_of_stack_ptr[0] = (uintptr_t)prot_domains_launch_kernel;
85  top_of_stack_ptr[1] = (uintptr_t)prot_domains_launch_app;
86 #endif
87 
88  /* Perform common GDT initialization */
89  gdt_init();
90 
91  /* Switch all data segment registers to the newly-initialized flat data
92  * descriptor.
93  */
94  __asm__(
95  "mov %0, %%ds\n\t"
96  "mov %0, %%es\n\t"
97  "mov %0, %%fs\n\t"
98  "mov %0, %%gs\n\t"
99  :
100  : "r" (GDT_SEL_DATA_FLAT)
101  );
102 
103  /**
104  * Perform specific GDT initialization tasks for the protection domain
105  * implementation that is enabled, if any.
106  */
107  prot_domains_gdt_init();
108 
109  /* Do not pass memory operands to the asm block below, since it is
110  * switching from the flat address space to a multi-segment address space
111  * model if such a model is used by the enabled protection domain
112  * implementation, if any.
113  */
114  __asm__(
115  "mov %[_ss_], %%ss\n\t"
116  "mov %[_esp_], %%esp\n\t"
117  "ljmp %[_cs_], %[_stage1_]\n\t"
118  :
119  : [_ss_] "r" (GDT_SEL_STK_EXC),
120  [_esp_] "r" (top_of_stack),
121  [_cs_] "i" ((uint16_t)GDT_SEL_CODE_EXC),
122  [_stage1_] "i" (boot_stage1)
123  );
124 }
static struct sicslowpan_addr_context * context
Addresses contexts for IPHC.
Definition: sicslowpan.c:501
int main(void)
This is main...
Definition: ethconfig.c:49