POK(kernelpart)
error.c
Go to the documentation of this file.
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Mon Jan 19 10:51:40 2009
15  */
16 
17 #ifdef POK_NEEDS_ERROR_HANDLING
18 
19 #include <types.h>
20 #include <core/thread.h>
21 #include <core/error.h>
22 #include <core/debug.h>
23 #include <core/partition.h>
24 
25 #include <libc.h>
26 
27 pok_ret_t pok_error_thread_create (uint32_t stack_size, void* entry)
28 {
29  uint32_t tid;
30  pok_thread_attr_t attr;
31  pok_ret_t ret;
32 
33  (void) stack_size;
34 
35  attr.priority = POK_THREAD_MAX_PRIORITY;
36  attr.entry = entry;
37  attr.time_capacity = 10000;
38 
39  ret = pok_partition_thread_create (&tid, &attr, POK_SCHED_CURRENT_PARTITION);
40 
41  POK_CURRENT_PARTITION.thread_error = tid;
42 
43  pok_sched_activate_error_thread ();
44 
45  return (ret);
46 }
47 
48 
54 void pok_error_declare (const uint8_t error)
55 {
61  if (POK_CURRENT_PARTITION.thread_error != 0)
62  {
63  POK_CURRENT_PARTITION.error_status.error_kind = error;
64  POK_CURRENT_PARTITION.error_status.failed_thread = POK_SCHED_CURRENT_THREAD - POK_CURRENT_PARTITION.thread_index_low;
65  POK_CURRENT_PARTITION.error_status.msg_size = 0;
66  /*
67  * FIXME: Add failed address and so on.
68  */
69  }
70  else
71  {
76  pok_partition_error (POK_SCHED_CURRENT_PARTITION, error);
77  }
78 }
79 
80 void pok_error_ignore ()
81 {
82  /* Do nothing at this time */
83 }
84 
85 
86 #ifndef POK_USE_GENERATED_KERNEL_ERROR_HANDLER
87 void pok_kernel_error (uint32_t error)
88 {
89 #ifdef POK_NEEDS_DEBUG
90  printf ("[KERNEL] [WARNING] Error %d was raised by the kernel but no error recovery was set\n", error);
91 #else
92  (void) error;
93 #endif /* POK_NEEDS_DEBUG */
94  return;
95 }
96 #endif
97 
98 #ifdef POK_NEEDS_PARTITIONS
99 #ifndef POK_USE_GENERATED_PARTITION_ERROR_HANDLER
100 void pok_partition_error (uint8_t partition, uint32_t error)
101 {
102 #ifdef POK_NEEDS_DEBUG
103  printf ("[KERNEL] [WARNING] Error %d was raised by partition %d but no error recovery was set\n", error, partition);
104 #else
105  (void) partition;
106  (void) error;
107 #endif /* POK_NEEDS_DEBUG */
108  return;
109 }
110 #endif /* POK_USE_GENERATED_PARTITION_ERROR_HANDLER */
111 #endif /* POK_NEEDS_PARTITIONS */
112 
113 
114 #ifndef POK_USE_GENERATED_KERNEL_ERROR_CALLBACK
115 void pok_error_kernel_callback ()
116 {
117 #ifdef POK_NEEDS_DEBUG
118  printf ("[KERNEL] [WARNING] Kernel calls callback function but nothing was defined by the user\n");
119  printf ("[KERNEL] [WARNING] You MUST define the pok_error_partition_callback function\n");
120 #endif /* POK_NEEDS_DEBUG */
121  return;
122 }
123 #endif /* POK_USE_GENERATED_KERNEL_ERROR_CALLBACK */
124 
125 
126 #ifdef POK_NEEDS_PARTITIONS
127 #ifndef POK_USE_GENERATED_PARTITION_ERROR_CALLBACK
128 void pok_error_partition_callback (uint32_t partition)
129 {
130 #ifdef POK_NEEDS_DEBUG
131  printf ("[KERNEL] [WARNING] Partition %d calls callback function but nothing was defined by the user\n", partition);
132  printf ("[KERNEL] [WARNING] You MUST define the pok_error_partition_callback function\n");
133 #else
134  (void) partition;
135 #endif
136  return;
137 }
138 #endif /* POK_USE_GENERATED_PARTITION_ERROR_CALLBACK */
139 
140 
141 void pok_error_raise_application_error (char* msg, uint32_t msg_size)
142 {
143  if (msg_size > POK_ERROR_MAX_MSG_SIZE)
144  {
145  msg_size = POK_ERROR_MAX_MSG_SIZE;
146  }
147 
148  pok_error_status_t* status;
149  status = &pok_partitions[pok_current_partition].error_status;
150  status->error_kind = POK_ERROR_KIND_APPLICATION_ERROR;
151  status->failed_thread = POK_SCHED_CURRENT_THREAD - POK_CURRENT_PARTITION.thread_index_low;
152  status->msg_size = msg_size;
153 
154  memcpy (status->msg, msg, msg_size);
155 
156  pok_sched_activate_error_thread ();
157 }
158 
159 pok_ret_t pok_error_get (pok_error_status_t* status)
160 {
161  if (POK_CURRENT_PARTITION.error_status.error_kind != POK_ERROR_KIND_INVALID)
162  {
163 
164  status->error_kind = POK_CURRENT_PARTITION.error_status.error_kind;
165  status->failed_thread = POK_CURRENT_PARTITION.error_status.failed_thread;
166  status->failed_addr = POK_CURRENT_PARTITION.error_status.failed_addr;
167  status->msg_size = POK_CURRENT_PARTITION.error_status.msg_size;
168  memcpy (status->msg, POK_CURRENT_PARTITION.error_status.msg, POK_CURRENT_PARTITION.error_status.msg_size);
169 
170  return POK_ERRNO_OK;
171  }
172  else
173  {
174  return POK_ERRNO_UNAVAILABLE;
175  }
176 }
177 
178 #endif /* POK_NEEDS_PARTITIONS */
179 
180 #endif
181