Skip to content

Commit 915fb12

Browse files
authored
Release v5.2.0
1 parent 2bbecdc commit 915fb12

File tree

9 files changed

+325
-57
lines changed

9 files changed

+325
-57
lines changed

CHANGELOG.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1+
## 5.2.0 (2024-08-22)
2+
* Add shared memory pthread mutex for multi-process and multi-threads applications for Linux.
3+
* Improve pal_os_event logging.
4+
* Improve logging for Linux UART.
5+
* Improve logging for Windows UART.
6+
17
## 5.1.0 (2024-07-29)
2-
* Improved signature encoding for secp521r1 signature
3-
* Improved logging output
8+
* Improved signature encoding for secp521r1 signature.
9+
* Improved logging output.
410
* Fixed race condition in pal_os_event callback in libusb PAL.
5-
* Added checks to signature padding check
11+
* Added checks to signature padding check.
612
* Added default configuration for MBedTLS.
7-
* Removed warnings during library compilation
13+
* Removed warnings during library compilation.
814
* Resolved initialization fails when triggering reset.
915

1016
## 5.0.1 (2024-05-21)
1117
* Fix to allow compilation using zephyr PAL.
1218
* Changes in README.md for host applications.
13-
* Added missing license headers
19+
* Added missing license headers.
1420

1521
## 5.0.0 (2024-04-10)
1622
* New folder structure.
File renamed without changes.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2019-2024 Infineon Technologies AG
3+
* SPDX-License-Identifier: MIT
4+
*
5+
* \author Infineon Technologies AG
6+
*
7+
* \file pal_shared_mutex.h
8+
*
9+
* \brief This file provides the prototype declarations shared memory acquire
10+
* and release.
11+
*
12+
* \ingroup grPAL
13+
* @{
14+
*/
15+
#ifndef _PAL_SHARED_MEM_MUTEX_H_
16+
#define _PAL_SHARED_MEM_MUTEX_H_
17+
18+
#include <pthread.h> // pthread_mutex_t, pthread_mutexattr_t,
19+
// pthread_mutexattr_init, pthread_mutexattr_setpshared,
20+
// pthread_mutex_init, pthread_mutex_destroy
21+
#include "pal.h"
22+
// Structure of a shared mutex.
23+
typedef struct shared_mutex_t {
24+
pthread_mutex_t *ptr; // Pointer to the pthread mutex and
25+
// shared memory segment.
26+
int shm_fd; // Descriptor of shared memory object.
27+
char *name; // Name of the mutex and associated
28+
// shared memory object.
29+
int created; // Equals 1 (true) if initialization
30+
// of this structure caused creation
31+
// of a new shared mutex.
32+
// Equals 0 (false) if this mutex was
33+
// just retrieved from shared memory.
34+
pid_t *pid; // PID of the process that previously seized the mutex
35+
36+
} shared_mutex_t;
37+
typedef struct trustm_mutex_t {
38+
pthread_mutex_t mutex;
39+
pid_t pid;
40+
} trustm_mutex_t;
41+
#define EMPTY_PID 0x55AA55AA
42+
43+
#define DEBUG_TRUSTM_MUTEX_L1 0
44+
#define DEBUG_TRUSTM_MUTEX_L2 0
45+
46+
#if DEBUG_TRUSTM_MUTEX_L1 == 1
47+
#define TRUSTM_MUTEX_DBGFN1(x, ...) \
48+
fprintf( \
49+
stdout, \
50+
"%d:%s:%d %s: " x "\n", \
51+
getpid(), \
52+
__FILE__, \
53+
__LINE__, \
54+
__FUNCTION__, \
55+
##__VA_ARGS__ \
56+
)
57+
#elif DEBUG_TRUSTM_MUTEX_L1 == 2
58+
59+
#define TRUSTM_MUTEX_DBGFN1(x, ...) \
60+
Log("%d:%s:%d %s: " x "\n", getpid(), __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
61+
#else
62+
#define TRUSTM_MUTEX_DBGFN1(x, ...)
63+
#endif
64+
65+
#if DEBUG_TRUSTM_MUTEX_L2 == 1
66+
#define TRUSTM_MUTEX_DBGFN2(x, ...) \
67+
fprintf( \
68+
stdout, \
69+
"%d:%s:%d %s: " x "\n", \
70+
getpid(), \
71+
__FILE__, \
72+
__LINE__, \
73+
__FUNCTION__, \
74+
##__VA_ARGS__ \
75+
)
76+
#elif DEBUG_TRUSTM_MUTEX_L2 == 2
77+
#define TRUSTM_MUTEX_DBGFN2(x, ...) \
78+
Log("%d:%s:%d %s: " x "\n", getpid(), __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
79+
#else
80+
#define TRUSTM_MUTEX_DBGFN2(x, ...)
81+
#endif
82+
83+
pal_status_t pal_shm_mutex_acquire(shared_mutex_t *shm_mutex, const char *mutex_name);
84+
void pal_shm_mutex_release(shared_mutex_t *shm_mutex);
85+
86+
#endif

extras/pal/linux/pal_os_event.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,27 @@
2424

2525
#include "pal_os_timer.h"
2626

27-
//#define TRUSTM_PAL_EVENT_DEBUG = 1
28-
29-
#ifdef TRUSTM_PAL_EVENT_DEBUG
27+
//~ #define TRUSTM_PAL_EVENT_DEBUG 1
3028

29+
#if TRUSTM_PAL_EVENT_DEBUG == 1
3130
#define TRUSTM_PAL_EVENT_DBG(x, ...) \
3231
fprintf(stderr, "%s:%d " x "\n", __FILE__, __LINE__, ##__VA_ARGS__)
3332
#define TRUSTM_PAL_EVENT_DBGFN(x, ...) \
3433
fprintf(stderr, "%s:%d %s: " x "\n", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
35-
#define TRUSTM_PAL_EVENT_ERRFN(x, ...) \
36-
fprintf(stderr, "Error in %s:%d %s: " x "\n", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
37-
#define TRUSTM_PAL_EVENT_MSGFN(x, ...) \
38-
fprintf(stderr, "Message:%s:%d %s: " x "\n", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
39-
34+
#elif TRUSTM_PAL_EVENT_DEBUG == 2
35+
#define TRUSTM_PAL_EVENT_DBG(x, ...) Log("%s:%d " x "\n", "Log", __LINE__, ##__VA_ARGS__)
36+
#define TRUSTM_PAL_EVENT_DBGFN(x, ...) \
37+
Log("%s:%d %s: " x "\n", "Log", __LINE__, __FUNCTION__, ##__VA_ARGS__)
4038
#else
41-
4239
#define TRUSTM_PAL_EVENT_DBG(x, ...)
4340
#define TRUSTM_PAL_EVENT_DBGFN(x, ...)
41+
#endif
42+
4443
#define TRUSTM_PAL_EVENT_ERRFN(x, ...) \
4544
fprintf(stderr, "Error in %s:%d %s: " x "\n", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
4645
#define TRUSTM_PAL_EVENT_MSGFN(x, ...) \
4746
fprintf(stderr, "Message:%s:%d %s: " x "\n", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
4847

49-
#endif
50-
5148
#define CLOCKID CLOCK_REALTIME
5249
#define SIG SIGRTMIN
5350

extras/pal/linux/pal_os_lock.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "pal_os_lock.h"
1616

17+
#include "include/pal_shared_mutex.h"
18+
shared_mutex_t trustm_mutex;
1719
void pal_os_lock_create(pal_os_lock_t *p_lock, uint8_t lock_type) {
1820
p_lock->type = lock_type;
1921
p_lock->lock = 0;
@@ -25,21 +27,15 @@ void pal_os_lock_destroy(pal_os_lock_t *p_lock) {}
2527

2628
pal_status_t pal_os_lock_acquire(pal_os_lock_t *p_lock) {
2729
pal_status_t return_status = PAL_STATUS_FAILURE;
28-
29-
if (!(p_lock->lock)) {
30-
p_lock->lock++;
31-
if (1 != p_lock->lock) {
32-
p_lock->lock--;
33-
}
34-
return_status = PAL_STATUS_SUCCESS;
35-
}
30+
return_status = pal_shm_mutex_acquire(&trustm_mutex, "/trustm-mutex");
31+
if (return_status == PAL_STATUS_SUCCESS)
32+
p_lock->lock = 1;
3633
return return_status;
3734
}
3835

3936
void pal_os_lock_release(pal_os_lock_t *p_lock) {
40-
if (0 != p_lock->lock) {
41-
p_lock->lock--;
42-
}
37+
pal_shm_mutex_release(&trustm_mutex);
38+
p_lock->lock = 0;
4339
}
4440

4541
void pal_os_lock_enter_critical_section() {}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2019-2024 Infineon Technologies AG
3+
* SPDX-License-Identifier: MIT
4+
*
5+
* \author Infineon Technologies AG
6+
*
7+
* \file pal_shared_mutex.c
8+
*
9+
* \brief This file implements the platform abstraction layer APIs for shared
10+
* memory mutex.
11+
*
12+
* \ingroup grPAL
13+
* @{
14+
*/
15+
16+
#include "include/pal_shared_mutex.h"
17+
18+
#include <errno.h> // errno, ENOENT
19+
#include <fcntl.h> // O_RDWR, O_CREATE
20+
#include <linux/limits.h> // NAME_MAX
21+
#include <sys/mman.h> // shm_open, shm_unlink, mmap, munmap,
22+
// PROT_READ, PROT_WRITE, MAP_SHARED, MAP_FAILED
23+
#include <stdio.h> // perror
24+
#include <stdlib.h> // malloc, free
25+
#include <string.h> // strcpy
26+
#include <unistd.h> // ftruncate, close
27+
28+
pthread_mutex_t shm_lock;
29+
30+
static shared_mutex_t shared_mutex_init(const char *name) {
31+
shared_mutex_t mutex = {NULL, 0, NULL, 0, NULL};
32+
trustm_mutex_t *addr;
33+
trustm_mutex_t *mutex_ptr;
34+
35+
// Open existing shared memory object, or create one.
36+
// Two separate calls are needed here, to mark fact of creation
37+
// for later initialization of pthread mutex.
38+
TRUSTM_MUTEX_DBGFN2(">");
39+
TRUSTM_MUTEX_DBGFN2("pthread lock");
40+
pthread_mutex_lock(&shm_lock);
41+
TRUSTM_MUTEX_DBGFN2("pthread lock successfully");
42+
mutex.shm_fd = shm_open(name, O_RDWR, 0660);
43+
if (mutex.shm_fd == -1 && errno == ENOENT) {
44+
mutex.shm_fd = shm_open(name, O_RDWR | O_CREAT, 0660);
45+
mutex.created = 1;
46+
TRUSTM_MUTEX_DBGFN2("create new shm");
47+
}
48+
TRUSTM_MUTEX_DBGFN2("pthread unlock");
49+
pthread_mutex_unlock(&shm_lock);
50+
TRUSTM_MUTEX_DBGFN2("pthread unlock successfully");
51+
if (mutex.shm_fd == -1) {
52+
perror("shm_open");
53+
return mutex;
54+
}
55+
TRUSTM_MUTEX_DBGFN2("truncate shm ");
56+
// Truncate shared memory segment so it would contain
57+
// trustm_mutex_t.
58+
if (ftruncate(mutex.shm_fd, sizeof(trustm_mutex_t)) != 0) {
59+
perror("ftruncate");
60+
return mutex;
61+
}
62+
63+
// Map pthread mutex into the shared memory.
64+
addr = mmap(NULL, sizeof(trustm_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED, mutex.shm_fd, 0);
65+
66+
if (addr == MAP_FAILED) {
67+
perror("mmap");
68+
return mutex;
69+
}
70+
mutex_ptr = addr;
71+
72+
// If shared memory was just initialized -
73+
// initialize the mutex as well.
74+
75+
if (mutex.created) {
76+
pthread_mutexattr_t attr;
77+
if (pthread_mutexattr_init(&attr)) {
78+
perror("pthread_mutexattr_init");
79+
return mutex;
80+
}
81+
if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)) {
82+
perror("pthread_mutexattr_setpshared");
83+
return mutex;
84+
}
85+
86+
if (pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST)) {
87+
perror("pthread_mutexattr_setrobust");
88+
return mutex;
89+
}
90+
91+
if (pthread_mutex_init(&mutex_ptr->mutex, &attr)) {
92+
perror("pthread_mutex_init");
93+
return mutex;
94+
}
95+
}
96+
TRUSTM_MUTEX_DBGFN2("-----> mutes ptr:%x", &mutex_ptr->mutex);
97+
#ifdef DEBUG_TRUSTM_MUTEX
98+
for (int i = 0; i < sizeof(pthread_mutex_t); i++) {
99+
printf("%x", ((char *)&mutex_ptr->mutex)[i]);
100+
}
101+
printf("\n");
102+
#endif
103+
mutex.ptr = &mutex_ptr->mutex;
104+
mutex.pid = &mutex_ptr->pid;
105+
mutex.name = (char *)malloc(NAME_MAX + 1);
106+
strcpy(mutex.name, name);
107+
TRUSTM_MUTEX_DBGFN2("<");
108+
return mutex;
109+
}
110+
111+
static int shared_mutex_close(shared_mutex_t mutex) {
112+
if (munmap((void *)mutex.ptr, sizeof(pthread_mutex_t))) {
113+
perror("munmap");
114+
return -1;
115+
}
116+
mutex.ptr = NULL;
117+
if (close(mutex.shm_fd)) {
118+
perror("close");
119+
return -1;
120+
}
121+
mutex.shm_fd = 0;
122+
free(mutex.name);
123+
return 0;
124+
}
125+
126+
static int shared_mutex_destroy(shared_mutex_t mutex) {
127+
if ((errno = pthread_mutex_destroy(mutex.ptr))) {
128+
perror("pthread_mutex_destroy");
129+
return -1;
130+
}
131+
if (munmap((void *)mutex.ptr, sizeof(pthread_mutex_t))) {
132+
perror("munmap");
133+
return -1;
134+
}
135+
mutex.ptr = NULL;
136+
if (close(mutex.shm_fd)) {
137+
perror("close");
138+
return -1;
139+
}
140+
mutex.shm_fd = 0;
141+
if (shm_unlink(mutex.name)) {
142+
perror("shm_unlink");
143+
return -1;
144+
}
145+
free(mutex.name);
146+
return 0;
147+
}
148+
149+
pal_status_t pal_shm_mutex_acquire(shared_mutex_t *shm_mutex, const char *mutex_name) {
150+
int result;
151+
TRUSTM_MUTEX_DBGFN1(">");
152+
153+
*shm_mutex = shared_mutex_init(mutex_name);
154+
if (shm_mutex->ptr == NULL) {
155+
TRUSTM_MUTEX_DBGFN1("Mutex create failed\n");
156+
return PAL_STATUS_FAILURE;
157+
}
158+
159+
result = pthread_mutex_lock(shm_mutex->ptr);
160+
TRUSTM_MUTEX_DBGFN1("Lock Mutex:%s: %x\n", mutex_name, (unsigned int)shm_mutex->ptr);
161+
if (result == EOWNERDEAD) {
162+
TRUSTM_MUTEX_DBGFN1("process owner dead, make it consistent\n");
163+
result = pthread_mutex_consistent(shm_mutex->ptr);
164+
if (result != 0) {
165+
perror("pthread_mutex_consistent error");
166+
return PAL_STATUS_FAILURE;
167+
}
168+
}
169+
if (shm_mutex->created) {
170+
TRUSTM_MUTEX_DBGFN1("The mutex was just created %x\n", *shm_mutex->pid);
171+
*shm_mutex->pid = EMPTY_PID;
172+
}
173+
TRUSTM_MUTEX_DBGFN1("<");
174+
return PAL_STATUS_SUCCESS;
175+
}
176+
177+
/**********************************************************************
178+
* pal_shm_mutex_release(shared_mutex_t *shm_mutex)
179+
**********************************************************************/
180+
void pal_shm_mutex_release(shared_mutex_t *shm_mutex) {
181+
TRUSTM_MUTEX_DBGFN1(">");
182+
pthread_mutex_unlock(shm_mutex->ptr);
183+
TRUSTM_MUTEX_DBGFN1("mutex unlock:%s:%x", shm_mutex->name, (unsigned int)shm_mutex->ptr);
184+
185+
shared_mutex_close(*shm_mutex);
186+
TRUSTM_MUTEX_DBGFN1("mutex closed");
187+
188+
TRUSTM_MUTEX_DBGFN1("<");
189+
}

0 commit comments

Comments
 (0)