aboutsummaryrefslogtreecommitdiff
path: root/frameworks/modular/odp_module.c
blob: 475bcd5e23b2f81a0ee962c807c72f97535c1943 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* Copyright (c) 2017, ARM Limited. All rights reserved.
 *
 * Copyright (c) 2017, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <config.h>

#include <stdio.h>
#include <errno.h>
#include "odp_module.h"

#define MODULE_FRAMEWORK_VERSION 0x00010000UL
ODP_SUBSYSTEM_DEFINE(module, "module framework", MODULE_FRAMEWORK_VERSION);

/* Bootstrap log facility, enable if ODP_DEBUG_PRINT flag is set. */
#define DBG(format, ...)					\
	do {							\
		if (ODP_DEBUG_PRINT == 1)			\
			fprintf(stderr, format, ##__VA_ARGS__);	\
	} while (0)

/* Keep it simple, allow one registration session at a time. */
static struct {
	odp_rwlock_t lock;
	odp_subsystem_t *subsystem;
	odp_module_base_t *module;
} registration = {
	.lock = ODP_RWLOCK_UNLOCKED,
	.subsystem = NULL,
	.module = NULL,
};

static inline int registration_sanity_check(
	odp_subsystem_t *subsystem, odp_module_base_t *module)
{
	if (subsystem == NULL || module == NULL)
		return -ENOENT;

	if (!list_node_detached(&module->list)) {
		DBG("module %s was already registered.\n", module->name);
		return -EAGAIN;
	}

	return 0;
}

/* Module is linked statically or dynamically, and are loaded by
 * program loader (execve) or dynamic linker/loader (ld.so)
 *
 * subsystem_register_module() should complete the whole registration
 * session and link the module into subsystem's module array.
 */
static int linker_register_module(
	odp_subsystem_t *subsystem, odp_module_base_t *module)
{
	int sanity = registration_sanity_check(subsystem, module);

	if (sanity < 0) /* sanity check errors */
		return sanity;

	/* Allow one registration session at a time */
	odp_rwlock_write_lock(&registration.lock);

	/* Block the subsystem API calls in load new
	 * implementation modules. */
	odp_rwlock_write_lock(&subsystem->lock);
	module->handler = NULL; /* no DSO handler */
	list_add_tail(&subsystem->modules, &module->list);
	odp_rwlock_write_unlock(&subsystem->lock);

	odp_rwlock_write_unlock(&registration.lock);
	return 0;
}

static int (*do_register_module)(odp_subsystem_t *, odp_module_base_t *)
		= &linker_register_module;

static int loader_register_module(
	odp_subsystem_t *subsystem, odp_module_base_t *module)
{
	int sanity = registration_sanity_check(subsystem, module);

	if (sanity < 0) /* sanity check errors */
		return sanity;

	/* Registration session lock must be held by
	 * module_loader_start(). */
	if (odp_rwlock_write_trylock(&registration.lock) == 0) {
		registration.subsystem = subsystem;
		registration.module = module;
		return 0;
	}

	odp_rwlock_write_unlock(&registration.lock);
	return -EACCES;
}

void odp_module_loader_start(void)
{
	odp_rwlock_write_lock(&registration.lock);

	if (registration.module != NULL ||
	    registration.subsystem != NULL) {
		DBG("module loader start warn, A previous "
		    "registration did not complete yet.\n");
	}

	registration.module = NULL;
	registration.subsystem = NULL;
	do_register_module = &loader_register_module;
}

void odp_module_loader_end(void)
{
	if (registration.module != NULL ||
	    registration.subsystem != NULL) {
		DBG("module loader end warn, A previous "
		    "registration did not complete yet.\n");
	}

	registration.module = NULL;
	registration.subsystem = NULL;
	do_register_module = &linker_register_module;

	odp_rwlock_write_unlock(&registration.lock);
}

int odp_module_install(void *dso, bool active)
{
	/* Bottom halves of the registration, context exclusion
	 * is guaranteed by module_loader_start()
	 */
	if (odp_rwlock_write_trylock(&registration.lock) == 0) {
		odp_subsystem_t *subsystem = registration.subsystem;
		odp_module_base_t *module = registration.module;

		if (subsystem != NULL && module != NULL) {
			odp_rwlock_write_lock(&subsystem->lock);

			module->handler = dso;
			list_add_tail(&subsystem->modules, &module->list);

			/* install as active implementation */
			if (active) /* warn: replaceable */
				subsystem->active = module;

			odp_rwlock_write_unlock(&subsystem->lock);
		}

		registration.subsystem = NULL;
		registration.module = NULL;
		return 0;
	}

	odp_rwlock_write_unlock(&registration.lock);
	return -EACCES;
}

int odp_module_abandon(void)
{
	/* Bottom halves of the registration, context exclusion
	 * is guaranteed by module_loader_start()
	 */
	if (odp_rwlock_write_trylock(&registration.lock) == 0) {
		registration.subsystem = NULL;
		registration.module = NULL;
		return 0;
	}

	odp_rwlock_write_unlock(&registration.lock);
	return -EACCES;
}

int __subsystem_register_module(
	odp_subsystem_t *subsystem, odp_module_base_t *module)
{
	return do_register_module(subsystem, module);
}