diff options
| author | Kimplul <kimi.h.kuparinen@gmail.com> | 2024-05-23 19:54:55 +0300 |
|---|---|---|
| committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2024-05-23 19:59:39 +0300 |
| commit | 6a7073e5f262db9a4578ff00b5b28e34335564ce (patch) | |
| tree | 38294048ff12b2abc6fc9c2fe967eae9d7b8c3b7 /common/tcb.c | |
| parent | c18ec832ed592e4c6171139a8aaadde827cb13da (diff) | |
| download | kmi-6a7073e5f262db9a4578ff00b5b28e34335564ce.tar.gz kmi-6a7073e5f262db9a4578ff00b5b28e34335564ce.zip | |
improve id handling
+ The number of allowed threads running at the same time is limited to
num_tids, but each thread's ID can be any larger than that. This
should make ID reuse a lot more rare, and probably makes certain kinds
of time-of-check-to-time-of-use attacks more difficult
Diffstat (limited to 'common/tcb.c')
| -rw-r--r-- | common/tcb.c | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/common/tcb.c b/common/tcb.c index 4b57c06..c0f82cc 100644 --- a/common/tcb.c +++ b/common/tcb.c @@ -23,7 +23,7 @@ /* arguably exessively many globals... */ /** Thread ID to start looking from when allocating new ID. */ -static id_t start_tid; +static id_t start_tid = 0; /** Total number of possible thread IDs. */ static id_t num_tids; @@ -46,6 +46,7 @@ void init_tcbs() * something smaller but this is fine for now. */ tcbs = (struct tcb **)alloc_page(MM_O1); num_tids = order_size(MM_O1) / sizeof(struct tcb *); + catastrophic_assert(is_powerof2(num_tids)); memset(tcbs, 0, order_size(MM_O1)); } @@ -64,18 +65,18 @@ static id_t __alloc_tid(struct tcb *t) { id_t stop_tid = start_tid - 1; /** \todo this would need some locking or something... */ - for (id_t i = start_tid; 1; ++i) { - if (i == ID_MAX) - i = 0; + for (id_t i = start_tid;; ++i) { + if (i <= 0) + i = 1; /* we're completely full */ if (i == stop_tid) return ERR_NF; - if (tcbs[i] || i == 0) + if (get_tcb(i) || i == 0) continue; - tcbs[i] = t; + tcbs[i & (num_tids - 1)] = t; start_tid = i + 1; return i; } @@ -292,10 +293,10 @@ struct tcb *get_tcb(id_t tid) { hard_assert(tcbs, 0); - if (tid <= 0 || tid >= num_tids) + if (tid <= 0) return NULL; - return tcbs[tid]; + return tcbs[tid & (num_tids - 1)]; } void set_return(struct tcb *t, vm_t v) |
