xref: /optee_os/core/tee/tee_obj.c (revision 316a94e710afc8dcb5b6ac991741ac6370af65fc)
1 /*
2  * Copyright (c) 2014, STMicroelectronics International N.V.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <tee/tee_obj.h>
29 
30 #include <stdlib.h>
31 #include <tee_api_defines.h>
32 #include <mm/tee_mmu.h>
33 #include <tee/tee_fs.h>
34 #include <tee/tee_pobj.h>
35 #include <trace.h>
36 
37 void tee_obj_add(struct tee_ta_ctx *ctx, struct tee_obj *o)
38 {
39 	TAILQ_INSERT_TAIL(&ctx->objects, o, link);
40 }
41 
42 TEE_Result tee_obj_get(struct tee_ta_ctx *ctx, uint32_t obj_id,
43 		       struct tee_obj **obj)
44 {
45 	struct tee_obj *o;
46 
47 	TAILQ_FOREACH(o, &ctx->objects, link) {
48 		if (obj_id == (vaddr_t)o) {
49 			*obj = o;
50 			return TEE_SUCCESS;
51 		}
52 	}
53 	return TEE_ERROR_BAD_PARAMETERS;
54 }
55 
56 void tee_obj_close(struct tee_ta_ctx *ctx, struct tee_obj *o)
57 {
58 	TAILQ_REMOVE(&ctx->objects, o, link);
59 
60 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) && o->fd >= 0) {
61 		tee_file_ops.close(o->fd);
62 		tee_pobj_release(o->pobj);
63 	}
64 
65 	if (o->cleanup)
66 		o->cleanup(o->data, true);
67 	free(o->data);
68 	free(o);
69 }
70 
71 void tee_obj_close_all(struct tee_ta_ctx *ctx)
72 {
73 	struct tee_obj_head *objects = &ctx->objects;
74 
75 	while (!TAILQ_EMPTY(objects))
76 		tee_obj_close(ctx, TAILQ_FIRST(objects));
77 }
78