Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * shctx.h - shared context management functions for SSL |
| 3 | * |
| 4 | * Copyright (C) 2011-2012 EXCELIANCE |
| 5 | * |
| 6 | * Author: Emeric Brun - emeric@exceliance.fr |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #ifndef SHCTX_H |
| 15 | #define SHCTX_H |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 16 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 17 | #include <common/mini-clist.h> |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 18 | #include <types/shctx.h> |
| 19 | |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 22 | #ifndef USE_PRIVATE_CACHE |
| 23 | #ifdef USE_PTHREAD_PSHARED |
| 24 | #include <pthread.h> |
| 25 | #else |
| 26 | #ifdef USE_SYSCALL_FUTEX |
| 27 | #include <unistd.h> |
| 28 | #include <linux/futex.h> |
| 29 | #include <sys/syscall.h> |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 30 | #endif |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 31 | #endif |
Emeric Brun | 786991e | 2012-11-26 18:37:12 +0100 | [diff] [blame] | 32 | #endif |
| 33 | |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 34 | int shctx_init(struct shared_context **orig_shctx, |
| 35 | int maxblocks, int blocksize, int maxobjsz, int extra, int shared); |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 36 | struct shared_block *shctx_row_reserve_hot(struct shared_context *shctx, |
| 37 | struct shared_block *last, int data_len); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 38 | void shctx_row_inc_hot(struct shared_context *shctx, struct shared_block *first); |
| 39 | void shctx_row_dec_hot(struct shared_context *shctx, struct shared_block *first); |
| 40 | int shctx_row_data_append(struct shared_context *shctx, |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 41 | struct shared_block *first, struct shared_block *from, |
| 42 | unsigned char *data, int len); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 43 | int shctx_row_data_get(struct shared_context *shctx, struct shared_block *first, |
| 44 | unsigned char *dst, int offset, int len); |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 45 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 46 | |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 47 | /* Lock functions */ |
| 48 | |
| 49 | #if defined (USE_PRIVATE_CACHE) |
| 50 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 51 | #define shctx_lock(shctx) |
| 52 | #define shctx_unlock(shctx) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 53 | |
| 54 | #elif defined (USE_PTHREAD_PSHARED) |
| 55 | extern int use_shared_mem; |
| 56 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 57 | #define shctx_lock(shctx) if (use_shared_mem) pthread_mutex_lock(&shctx->mutex) |
| 58 | #define shctx_unlock(shctx) if (use_shared_mem) pthread_mutex_unlock(&shctx->mutex) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 59 | |
| 60 | #else |
| 61 | extern int use_shared_mem; |
| 62 | |
| 63 | #ifdef USE_SYSCALL_FUTEX |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 64 | static inline void _shctx_wait4lock(unsigned int *count, unsigned int *uaddr, int value) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 65 | { |
| 66 | syscall(SYS_futex, uaddr, FUTEX_WAIT, value, NULL, 0, 0); |
| 67 | } |
| 68 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 69 | static inline void _shctx_awakelocker(unsigned int *uaddr) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 70 | { |
| 71 | syscall(SYS_futex, uaddr, FUTEX_WAKE, 1, NULL, 0, 0); |
| 72 | } |
| 73 | |
| 74 | #else /* internal spin lock */ |
| 75 | |
| 76 | #if defined (__i486__) || defined (__i586__) || defined (__i686__) || defined (__x86_64__) |
| 77 | static inline void relax() |
| 78 | { |
| 79 | __asm volatile("rep;nop\n" ::: "memory"); |
| 80 | } |
| 81 | #else /* if no x86_64 or i586 arch: use less optimized but generic asm */ |
| 82 | static inline void relax() |
| 83 | { |
| 84 | __asm volatile("" ::: "memory"); |
| 85 | } |
| 86 | #endif |
| 87 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 88 | static inline void _shctx_wait4lock(unsigned int *count, unsigned int *uaddr, int value) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 89 | { |
| 90 | int i; |
| 91 | |
| 92 | for (i = 0; i < *count; i++) { |
| 93 | relax(); |
| 94 | relax(); |
| 95 | } |
| 96 | *count = *count << 1; |
| 97 | } |
| 98 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 99 | #define _shctx_awakelocker(a) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 100 | |
| 101 | #endif |
| 102 | |
| 103 | #if defined (__i486__) || defined (__i586__) || defined (__i686__) || defined (__x86_64__) |
| 104 | static inline unsigned int xchg(unsigned int *ptr, unsigned int x) |
| 105 | { |
| 106 | __asm volatile("lock xchgl %0,%1" |
| 107 | : "=r" (x), "+m" (*ptr) |
| 108 | : "0" (x) |
| 109 | : "memory"); |
| 110 | return x; |
| 111 | } |
| 112 | |
| 113 | static inline unsigned int cmpxchg(unsigned int *ptr, unsigned int old, unsigned int new) |
| 114 | { |
| 115 | unsigned int ret; |
| 116 | |
| 117 | __asm volatile("lock cmpxchgl %2,%1" |
| 118 | : "=a" (ret), "+m" (*ptr) |
| 119 | : "r" (new), "0" (old) |
| 120 | : "memory"); |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | static inline unsigned char atomic_dec(unsigned int *ptr) |
| 125 | { |
| 126 | unsigned char ret; |
| 127 | __asm volatile("lock decl %0\n" |
| 128 | "setne %1\n" |
| 129 | : "+m" (*ptr), "=qm" (ret) |
| 130 | : |
| 131 | : "memory"); |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | #else /* if no x86_64 or i586 arch: use less optimized gcc >= 4.1 built-ins */ |
| 136 | static inline unsigned int xchg(unsigned int *ptr, unsigned int x) |
| 137 | { |
| 138 | return __sync_lock_test_and_set(ptr, x); |
| 139 | } |
| 140 | |
| 141 | static inline unsigned int cmpxchg(unsigned int *ptr, unsigned int old, unsigned int new) |
| 142 | { |
| 143 | return __sync_val_compare_and_swap(ptr, old, new); |
| 144 | } |
| 145 | |
| 146 | static inline unsigned char atomic_dec(unsigned int *ptr) |
| 147 | { |
| 148 | return __sync_sub_and_fetch(ptr, 1) ? 1 : 0; |
| 149 | } |
| 150 | |
| 151 | #endif |
| 152 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 153 | static inline void _shctx_lock(struct shared_context *shctx) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 154 | { |
| 155 | unsigned int x; |
| 156 | unsigned int count = 4; |
| 157 | |
| 158 | x = cmpxchg(&shctx->waiters, 0, 1); |
| 159 | if (x) { |
| 160 | if (x != 2) |
| 161 | x = xchg(&shctx->waiters, 2); |
| 162 | |
| 163 | while (x) { |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 164 | _shctx_wait4lock(&count, &shctx->waiters, 2); |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 165 | x = xchg(&shctx->waiters, 2); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 170 | static inline void _shctx_unlock(struct shared_context *shctx) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 171 | { |
| 172 | if (atomic_dec(&shctx->waiters)) { |
| 173 | shctx->waiters = 0; |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 174 | _shctx_awakelocker(&shctx->waiters); |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 178 | #define shctx_lock(shctx) if (use_shared_mem) _shctx_lock(shctx) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 179 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 180 | #define shctx_unlock(shctx) if (use_shared_mem) _shctx_unlock(shctx) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 181 | |
| 182 | #endif |
| 183 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 184 | /* List Macros */ |
| 185 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 186 | /* |
| 187 | * Insert <s> block after <head> which is not necessarily the head of a list, |
| 188 | * so between <head> and the next element after <head>. |
| 189 | */ |
| 190 | static inline void shctx_block_append_hot(struct shared_context *shctx, |
| 191 | struct list *head, |
| 192 | struct shared_block *s) |
| 193 | { |
| 194 | shctx->nbav--; |
| 195 | LIST_DEL(&s->list); |
| 196 | LIST_ADD(head, &s->list); |
| 197 | } |
| 198 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 199 | static inline void shctx_block_set_hot(struct shared_context *shctx, |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 200 | struct shared_block *s) |
| 201 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 202 | shctx->nbav--; |
| 203 | LIST_DEL(&s->list); |
| 204 | LIST_ADDQ(&shctx->hot, &s->list); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 205 | } |
| 206 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 207 | static inline void shctx_block_set_avail(struct shared_context *shctx, |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 208 | struct shared_block *s) |
| 209 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 210 | shctx->nbav++; |
| 211 | LIST_DEL(&s->list); |
| 212 | LIST_ADDQ(&shctx->avail, &s->list); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 213 | } |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 214 | |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 215 | #endif /* SHCTX_H */ |
| 216 | |