xref: /haiku/src/system/kernel/image.cpp (revision 40e20c10768c3dcfc54074b8886e3e91455332e1)
1 /*
2  * Copyright 2003-2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 /*! User Runtime Loader support in the kernel */
7 
8 
9 #include <KernelExport.h>
10 
11 #include <kernel.h>
12 #include <kimage.h>
13 #include <kscheduler.h>
14 #include <lock.h>
15 #include <Notifications.h>
16 #include <team.h>
17 #include <thread.h>
18 #include <thread_types.h>
19 #include <user_debugger.h>
20 #include <util/AutoLock.h>
21 
22 #include <stdlib.h>
23 #include <string.h>
24 
25 
26 //#define TRACE_IMAGE
27 #ifdef TRACE_IMAGE
28 #	define TRACE(x) dprintf x
29 #else
30 #	define TRACE(x) ;
31 #endif
32 
33 #define ADD_DEBUGGER_COMMANDS
34 
35 
36 struct ImageTableDefinition {
37 	typedef image_id		KeyType;
38 	typedef struct image	ValueType;
39 
40 	size_t HashKey(image_id key) const { return key; }
41 	size_t Hash(struct image* value) const { return value->info.id; }
42 	bool Compare(image_id key, struct image* value) const
43 		{ return value->info.id == key; }
44 	struct image*& GetLink(struct image* value) const
45 		{ return value->hash_link; }
46 };
47 
48 typedef BOpenHashTable<ImageTableDefinition> ImageTable;
49 
50 
51 class ImageNotificationService : public DefaultNotificationService {
52 public:
53 	ImageNotificationService()
54 		: DefaultNotificationService("images")
55 	{
56 	}
57 
58 	void Notify(uint32 eventCode, struct image* image)
59 	{
60 		char eventBuffer[128];
61 		KMessage event;
62 		event.SetTo(eventBuffer, sizeof(eventBuffer), IMAGE_MONITOR);
63 		event.AddInt32("event", eventCode);
64 		event.AddInt32("image", image->info.id);
65 		event.AddPointer("imageStruct", image);
66 
67 		DefaultNotificationService::Notify(event, eventCode);
68 	}
69 };
70 
71 
72 static image_id sNextImageID = 1;
73 static mutex sImageMutex = MUTEX_INITIALIZER("image");
74 static ImageTable* sImageTable;
75 static ImageNotificationService sNotificationService;
76 
77 
78 /*!	Registers an image with the specified team.
79 */
80 image_id
81 register_image(Team *team, image_info *_info, size_t size)
82 {
83 	image_id id = atomic_add(&sNextImageID, 1);
84 	struct image *image;
85 
86 	image = (struct image*)malloc(sizeof(struct image));
87 	if (image == NULL)
88 		return B_NO_MEMORY;
89 
90 	memcpy(&image->info, _info, sizeof(image_info));
91 	image->team = team->id;
92 
93 	mutex_lock(&sImageMutex);
94 
95 	image->info.id = id;
96 
97 	// Add the app image to the head of the list. Some code relies on it being
98 	// the first image to be returned by get_next_image_info().
99 	if (image->info.type == B_APP_IMAGE)
100 		list_add_link_to_head(&team->image_list, image);
101 	else
102 		list_add_item(&team->image_list, image);
103 	sImageTable->Insert(image);
104 
105 	// notify listeners
106 	sNotificationService.Notify(IMAGE_ADDED, image);
107 
108 	mutex_unlock(&sImageMutex);
109 
110 	TRACE(("register_image(team = %p, image id = %ld, image = %p\n", team, id, image));
111 	return id;
112 }
113 
114 
115 /*!	Unregisters an image from the specified team.
116 */
117 status_t
118 unregister_image(Team *team, image_id id)
119 {
120 	status_t status = B_ENTRY_NOT_FOUND;
121 
122 	mutex_lock(&sImageMutex);
123 
124 	struct image *image = sImageTable->Lookup(id);
125 	if (image != NULL && image->team == team->id) {
126 		list_remove_link(image);
127 		sImageTable->Remove(image);
128 		status = B_OK;
129 	}
130 
131 	mutex_unlock(&sImageMutex);
132 
133 	if (status == B_OK) {
134 		// notify the debugger
135 		user_debug_image_deleted(&image->info);
136 
137 		// notify listeners
138 		sNotificationService.Notify(IMAGE_REMOVED, image);
139 
140 		free(image);
141 	}
142 
143 	return status;
144 }
145 
146 
147 /*!	Counts the registered images from the specified team.
148 	Interrupts must be enabled.
149 */
150 int32
151 count_images(Team *team)
152 {
153 	struct image *image = NULL;
154 	int32 count = 0;
155 
156 	MutexLocker locker(sImageMutex);
157 
158 	while ((image = (struct image*)list_get_next_item(&team->image_list, image))
159 			!= NULL) {
160 		count++;
161 	}
162 
163 	return count;
164 }
165 
166 
167 /*!	Removes all images from the specified team. Must only be called
168 	with a team that has already been removed from the list (in thread_exit()).
169 */
170 status_t
171 remove_images(Team *team)
172 {
173 	struct image *image;
174 
175 	ASSERT(team != NULL);
176 
177 	mutex_lock(&sImageMutex);
178 
179 	while ((image = (struct image*)list_remove_head_item(&team->image_list))
180 			!= NULL) {
181 		sImageTable->Remove(image);
182 		free(image);
183 	}
184 
185 	mutex_unlock(&sImageMutex);
186 
187 	return B_OK;
188 }
189 
190 
191 status_t
192 _get_image_info(image_id id, image_info *info, size_t size)
193 {
194 	if (size > sizeof(image_info))
195 		return B_BAD_VALUE;
196 
197 	status_t status = B_ENTRY_NOT_FOUND;
198 
199 	mutex_lock(&sImageMutex);
200 
201 	struct image *image = sImageTable->Lookup(id);
202 	if (image != NULL) {
203 		memcpy(info, &image->info, size);
204 		status = B_OK;
205 	}
206 
207 	mutex_unlock(&sImageMutex);
208 
209 	return status;
210 }
211 
212 
213 status_t
214 _get_next_image_info(team_id teamID, int32 *cookie, image_info *info,
215 	size_t size)
216 {
217 	if (size > sizeof(image_info))
218 		return B_BAD_VALUE;
219 
220 	// get the team
221 	Team* team = Team::Get(teamID);
222 	if (team == NULL)
223 		return B_BAD_TEAM_ID;
224 	BReference<Team> teamReference(team, true);
225 
226 	// iterate through the team's images
227 	MutexLocker imageLocker(sImageMutex);
228 
229 	struct image* image = NULL;
230 	int32 count = 0;
231 
232 	while ((image = (struct image*)list_get_next_item(&team->image_list,
233 			image)) != NULL) {
234 		if (count == *cookie) {
235 			memcpy(info, &image->info, size);
236 			(*cookie)++;
237 			return B_OK;
238 		}
239 		count++;
240 	}
241 
242 	return B_ENTRY_NOT_FOUND;
243 }
244 
245 
246 #ifdef ADD_DEBUGGER_COMMANDS
247 static int
248 dump_images_list(int argc, char **argv)
249 {
250 	struct image *image = NULL;
251 	Team *team;
252 
253 	if (argc > 1) {
254 		team_id id = strtol(argv[1], NULL, 0);
255 		team = team_get_team_struct_locked(id);
256 		if (team == NULL) {
257 			kprintf("No team with ID %" B_PRId32 " found\n", id);
258 			return 1;
259 		}
260 	} else
261 		team = thread_get_current_thread()->team;
262 
263 	kprintf("Registered images of team %" B_PRId32 "\n", team->id);
264 	kprintf("    ID text       size    data       size    name\n");
265 
266 	while ((image = (struct image*)list_get_next_item(&team->image_list, image))
267 			!= NULL) {
268 		image_info *info = &image->info;
269 
270 		kprintf("%6" B_PRId32 " %p %-7" B_PRId32 " %p %-7" B_PRId32 " %s\n",
271 			info->id, info->text, info->text_size, info->data, info->data_size,
272 			info->name);
273 	}
274 
275 	return 0;
276 }
277 #endif
278 
279 
280 struct image*
281 image_iterate_through_images(image_iterator_callback callback, void* cookie)
282 {
283 	MutexLocker locker(sImageMutex);
284 
285 	ImageTable::Iterator it = sImageTable->GetIterator();
286 	struct image* image = NULL;
287 	while ((image = it.Next()) != NULL) {
288 		if (callback(image, cookie))
289 			break;
290 	}
291 
292 	return image;
293 }
294 
295 
296 status_t
297 image_debug_lookup_user_symbol_address(Team *team, addr_t address,
298 	addr_t *_baseAddress, const char **_symbolName, const char **_imageName,
299 	bool *_exactMatch)
300 {
301 	// TODO: work together with ELF reader and runtime_loader
302 
303 	struct image *image = NULL;
304 
305 	while ((image = (struct image*)list_get_next_item(&team->image_list, image))
306 			!= NULL) {
307 		image_info *info = &image->info;
308 
309 		if ((address < (addr_t)info->text
310 				|| address >= (addr_t)info->text + info->text_size)
311 			&& (address < (addr_t)info->data
312 				|| address >= (addr_t)info->data + info->data_size))
313 			continue;
314 
315 		// found image
316 		*_symbolName = NULL;
317 		*_imageName = info->name;
318 		*_baseAddress = (addr_t)info->text;
319 		*_exactMatch = false;
320 
321 		return B_OK;
322 	}
323 
324 	return B_ENTRY_NOT_FOUND;
325 }
326 
327 
328 status_t
329 image_init(void)
330 {
331 	sImageTable = new(std::nothrow) ImageTable;
332 	if (sImageTable == NULL) {
333 		panic("image_init(): Failed to allocate image table!");
334 		return B_NO_MEMORY;
335 	}
336 
337 	status_t error = sImageTable->Init();
338 	if (error != B_OK) {
339 		panic("image_init(): Failed to init image table: %s", strerror(error));
340 		return error;
341 	}
342 
343 	new(&sNotificationService) ImageNotificationService();
344 
345 #ifdef ADD_DEBUGGER_COMMANDS
346 	add_debugger_command("team_images", &dump_images_list, "Dump all registered images from the current team");
347 #endif
348 
349 	return B_OK;
350 }
351 
352 
353 static void
354 notify_loading_app(status_t result, bool suspend)
355 {
356 	Team* team = thread_get_current_thread()->team;
357 
358 	TeamLocker teamLocker(team);
359 
360 	if (team->loading_info) {
361 		// there's indeed someone waiting
362 		struct team_loading_info* loadingInfo = team->loading_info;
363 		team->loading_info = NULL;
364 
365 		loadingInfo->result = result;
366 		loadingInfo->done = true;
367 
368 		// we're done with the team stuff, get the scheduler lock instead
369 		teamLocker.Unlock();
370 		InterruptsSpinLocker schedulerLocker(gSchedulerLock);
371 
372 		// wake up the waiting thread
373 		if (loadingInfo->thread->state == B_THREAD_SUSPENDED)
374 			scheduler_enqueue_in_run_queue(loadingInfo->thread);
375 
376 		// suspend ourselves, if desired
377 		if (suspend) {
378 			thread_get_current_thread()->next_state = B_THREAD_SUSPENDED;
379 			scheduler_reschedule();
380 		}
381 	}
382 }
383 
384 
385 //	#pragma mark -
386 //	Functions exported for the user space
387 
388 
389 status_t
390 _user_unregister_image(image_id id)
391 {
392 	return unregister_image(thread_get_current_thread()->team, id);
393 }
394 
395 
396 image_id
397 _user_register_image(image_info *userInfo, size_t size)
398 {
399 	image_info info;
400 
401 	if (size != sizeof(image_info))
402 		return B_BAD_VALUE;
403 
404 	if (!IS_USER_ADDRESS(userInfo)
405 		|| user_memcpy(&info, userInfo, size) < B_OK)
406 		return B_BAD_ADDRESS;
407 
408 	return register_image(thread_get_current_thread()->team, &info, size);
409 }
410 
411 
412 void
413 _user_image_relocated(image_id id)
414 {
415 	image_info info;
416 	status_t error;
417 
418 	// get an image info
419 	error = _get_image_info(id, &info, sizeof(image_info));
420 	if (error != B_OK) {
421 		dprintf("_user_image_relocated(%" B_PRId32 "): Failed to get image "
422 			"info: %" B_PRIx32 "\n", id, error);
423 		return;
424 	}
425 
426 	// notify the debugger
427 	user_debug_image_created(&info);
428 
429 	// If the image is the app image, loading is done. We need to notify the
430 	// thread who initiated the process and is now waiting for us to be done.
431 	if (info.type == B_APP_IMAGE)
432 		notify_loading_app(B_OK, true);
433 }
434 
435 
436 void
437 _user_loading_app_failed(status_t error)
438 {
439 	if (error >= B_OK)
440 		error = B_ERROR;
441 
442 	notify_loading_app(error, false);
443 
444 	_user_exit_team(error);
445 }
446 
447 
448 status_t
449 _user_get_image_info(image_id id, image_info *userInfo, size_t size)
450 {
451 	image_info info;
452 	status_t status;
453 
454 	if (size > sizeof(image_info))
455 		return B_BAD_VALUE;
456 
457 	if (!IS_USER_ADDRESS(userInfo))
458 		return B_BAD_ADDRESS;
459 
460 	status = _get_image_info(id, &info, sizeof(image_info));
461 
462 	if (user_memcpy(userInfo, &info, size) < B_OK)
463 		return B_BAD_ADDRESS;
464 
465 	return status;
466 }
467 
468 
469 status_t
470 _user_get_next_image_info(team_id team, int32 *_cookie, image_info *userInfo,
471 	size_t size)
472 {
473 	image_info info;
474 	status_t status;
475 
476 	if (size > sizeof(image_info))
477 		return B_BAD_VALUE;
478 
479 	if (!IS_USER_ADDRESS(userInfo) || !IS_USER_ADDRESS(_cookie))
480 		return B_BAD_ADDRESS;
481 
482 	status = _get_next_image_info(team, _cookie, &info, sizeof(image_info));
483 
484 	if (user_memcpy(userInfo, &info, size) < B_OK)
485 		return B_BAD_ADDRESS;
486 
487 	return status;
488 }
489 
490