• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 #include "drv_index_mgr.h"
13 #include <pthread.h>
14 #include <tee_log.h>
15 #include <tee_bitmap.h>
16 #include "drv_fd_ops.h"
17 
18 static uint8_t g_drv_index_bitmap[(DRV_INDEX_MAX) >> MOVE_BIT];
19 static pthread_mutex_t g_drv_index_mtx = PTHREAD_MUTEX_INITIALIZER;
20 
alloc_drv_index(void)21 int32_t alloc_drv_index(void)
22 {
23     int32_t ret = drv_mutex_lock(&g_drv_index_mtx);
24     if (ret != 0) {
25         tloge("get drv index mtx failed\n");
26         return -1;
27     }
28 
29     int32_t drv_index = get_valid_bit(g_drv_index_bitmap, DRV_INDEX_MAX);
30     if (drv_index == -1) {
31         tloge("cannot get drv index bit\n");
32         ret = pthread_mutex_unlock(&g_drv_index_mtx);
33         if (ret != 0)
34             tloge("something wrong, unlock mtx in drv index failed 0x%x\n", ret);
35         return -1;
36     }
37 
38     set_bitmap(g_drv_index_bitmap, DRV_INDEX_MAX, drv_index);
39 
40     ret = pthread_mutex_unlock(&g_drv_index_mtx);
41     if (ret != 0)
42         tloge("something wrong, unlock mtx in drv index failed 0x%x\n", ret);
43 
44     return (drv_index + 1);
45 }
46 
clear_drv_index(int32_t drv_index)47 void clear_drv_index(int32_t drv_index)
48 {
49     if (drv_index <= 0 || drv_index > DRV_INDEX_MAX) {
50         tloge("invalid drv_index:0x%x\n", drv_index);
51         return;
52     }
53 
54     int32_t ret = drv_mutex_lock(&g_drv_index_mtx);
55     if (ret != 0) {
56         tloge("get drv index mtx failed\n");
57         return;
58     }
59 
60     clear_bitmap(g_drv_index_bitmap, DRV_INDEX_MAX, (drv_index - 1));
61 
62     ret = pthread_mutex_unlock(&g_drv_index_mtx);
63     if (ret != 0)
64         tloge("something wrong, unlock mtx in drv index failed 0x%x\n", ret);
65 }
66