]> granicus.if.org Git - esp-idf/blob - components/esp32/ipc.c
fix buffer overflow in ipc.c
[esp-idf] / components / esp32 / ipc.c
1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stddef.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <assert.h>
19 #include "esp_err.h"
20 #include "esp_ipc.h"
21 #include "esp_attr.h"
22
23 #include "freertos/FreeRTOS.h"
24 #include "freertos/task.h"
25 #include "freertos/semphr.h"
26
27
28 static TaskHandle_t s_ipc_tasks[portNUM_PROCESSORS];         // Two high priority tasks, one for each CPU
29 static SemaphoreHandle_t s_ipc_mutex;                        // This mutex is used as a global lock for esp_ipc_* APIs
30 static SemaphoreHandle_t s_ipc_sem[portNUM_PROCESSORS];      // Two semaphores used to wake each of s_ipc_tasks
31 static SemaphoreHandle_t s_ipc_ack;                          // Semaphore used to acknowledge that task was woken up,
32                                                              //   or function has finished running
33 static volatile esp_ipc_func_t s_func;                       // Function which should be called by high priority task
34 static void * volatile s_func_arg;                           // Argument to pass into s_func
35 typedef enum {
36     IPC_WAIT_FOR_START,
37     IPC_WAIT_FOR_END
38 } esp_ipc_wait_t;
39
40 static volatile esp_ipc_wait_t s_ipc_wait;                   // This variable tells high priority task when it should give
41                                                              //   s_ipc_ack semaphore: before s_func is called, or
42                                                              //   after it returns
43
44 static void IRAM_ATTR ipc_task(void* arg)
45 {
46     const uint32_t cpuid = (uint32_t) arg;
47     assert(cpuid == xPortGetCoreID());
48     while (true) {
49         // Wait for IPC to be initiated.
50         // This will be indicated by giving the semaphore corresponding to
51         // this CPU.
52         if (xSemaphoreTake(s_ipc_sem[cpuid], portMAX_DELAY) != pdTRUE) {
53             // TODO: when can this happen?
54             abort();
55         }
56
57         esp_ipc_func_t func = s_func;
58         void* arg = s_func_arg;
59
60         if (s_ipc_wait == IPC_WAIT_FOR_START) {
61             xSemaphoreGive(s_ipc_ack);
62         }
63         (*func)(arg);
64         if (s_ipc_wait == IPC_WAIT_FOR_END) {
65             xSemaphoreGive(s_ipc_ack);
66         }
67     }
68     // TODO: currently this is unreachable code. Introduce esp_ipc_uninit
69     // function which will signal to both tasks that they can shut down.
70     // Not critical at this point, we don't have a use case for stopping
71     // IPC yet.
72     // Also need to delete the semaphore here.
73     vTaskDelete(NULL);
74 }
75
76 void esp_ipc_init()
77 {
78     s_ipc_mutex = xSemaphoreCreateMutex();
79     s_ipc_ack = xSemaphoreCreateBinary();
80     char task_name[8];
81     for (int i = 0; i < portNUM_PROCESSORS; ++i) {
82         sprintf(task_name,"ipc%d",i);
83         s_ipc_sem[i] = xSemaphoreCreateBinary();
84         portBASE_TYPE res = xTaskCreatePinnedToCore(ipc_task, task_name, CONFIG_IPC_TASK_STACK_SIZE, (void*) i,
85                                                     configMAX_PRIORITIES - 1, &s_ipc_tasks[i], i);
86         assert(res == pdTRUE);
87     }
88 }
89
90 static esp_err_t esp_ipc_call_and_wait(uint32_t cpu_id, esp_ipc_func_t func, void* arg, esp_ipc_wait_t wait_for)
91 {
92     if (cpu_id >= portNUM_PROCESSORS) {
93         return ESP_ERR_INVALID_ARG;
94     }
95     if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
96         return ESP_ERR_INVALID_STATE;
97     }
98
99     xSemaphoreTake(s_ipc_mutex, portMAX_DELAY);
100
101     s_func = func;
102     s_func_arg = arg;
103     s_ipc_wait = IPC_WAIT_FOR_START;
104     xSemaphoreGive(s_ipc_sem[cpu_id]);
105     xSemaphoreTake(s_ipc_ack, portMAX_DELAY);
106     xSemaphoreGive(s_ipc_mutex);
107     return ESP_OK;
108 }
109
110 esp_err_t esp_ipc_call(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
111 {
112     return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_START);
113 }
114
115 esp_err_t esp_ipc_call_blocking(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
116 {
117     return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_END);
118 }
119