1. binder基本概念

1.1 特点

1)binder 是一种基于C/S通信模式的IPC(Inter_Process Communication)。
2)在传输过程中近需要一次copy,为发送添加UID/PID身份,既支持实名binder也支持匿名binder,安全性高
3)binder驱动是标准的linux驱动,但是不是驱动专门的硬件,而是虚拟出一个binder管理设备,用于管理binder通信。

1.2

2. binder驱动源码分析

2.1 binder初始化函数binder_init

static int __init binder_init(void)
{
int ret;
char *device_name, *device_tmp;
struct binder_device *device;
struct hlist_node *tmp;
char *device_names = NULL;

ret = binder_alloc_shrinker_init();//初始化binder缓冲区
if (ret)
	return ret;

atomic_set(&binder_transaction_log.cur, ~0U);
atomic_set(&binder_transaction_log_failed.cur, ~0U);

binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
if (binder_debugfs_dir_entry_root)
	binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
					 binder_debugfs_dir_entry_root);

if (binder_debugfs_dir_entry_root) {
	debugfs_create_file("state",
			    0444,
			    binder_debugfs_dir_entry_root,
			    NULL,
			    &binder_state_fops);
	debugfs_create_file("stats",
			    0444,
			    binder_debugfs_dir_entry_root,
			    NULL,
			    &binder_stats_fops);
	debugfs_create_file("transactions",
			    0444,
			    binder_debugfs_dir_entry_root,
			    NULL,
			    &binder_transactions_fops);
	debugfs_create_file("transaction_log",
			    0444,
			    binder_debugfs_dir_entry_root,
			    &binder_transaction_log,
			    &binder_transaction_log_fops);
	debugfs_create_file("failed_transaction_log",
			    0444,
			    binder_debugfs_dir_entry_root,
			    &binder_transaction_log_failed,
			    &binder_transaction_log_fops);
}

if (!IS_ENABLED(CONFIG_ANDROID_BINDERFS) &&
    strcmp(binder_devices_param, "") != 0) {
	/*
	* Copy the module_parameter string, because we don't want to
	* tokenize it in-place.
	 */
	device_names = kstrdup(binder_devices_param, GFP_KERNEL);//给device_names申请空间
	if (!device_names) {
		ret = -ENOMEM;
		goto err_alloc_device_names_failed;
	}

	device_tmp = device_names;
	while ((device_name = strsep(&device_tmp, ","))) {
		ret = init_binder_device(device_name); //注册misc设备
		if (ret)
			goto err_init_binder_device_failed;
	}
}

ret = init_binderfs();
if (ret)
	goto err_init_binder_device_failed;

return ret;

err_init_binder_device_failed:
hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
misc_deregister(&device->miscdev);
hlist_del(&device->hlist);
kfree(device);
}

kfree(device_names);

err_alloc_device_names_failed:
debugfs_remove_recursive(binder_debugfs_dir_entry_root);

return ret;

}
binder_init函数有三个作用:
1) 初始化binder misc设备
2) 给设备分配内存
3) 调用list_add,将misc device设备放入设备列表中misc_list

2.2 binder初始化函数binder_init

posted on 2022-05-15 18:33  菜鸟变形记  阅读(219)  评论(0)    收藏  举报