binder 驱动的操作

以下源码均来自
https://github.com/aosp-mirror/kernel_common.git

Binder驱动四个基本操作

在进程启动的时候跟binder驱动交互有四个操作:

init -> open -> mmap -> ioctl

  • init: 初始化驱动

  • open: 打开驱动

  • mmap: 在内核分配binder_buffer

  • ioctl: 进程跟驱动的操作交互

初始化字符设备

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;
    // 初始化 binder缓冲区
    ret = binder_alloc_shrinker_init();
    // 创建binder目录 /binder/proc
    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) {
   
       // 创建binder/proc/state文件 
       debugfs_create_file("state",
                    0444,
                    binder_debugfs_dir_entry_root,
                    NULL,
                    &binder_state_fops);
       // 创建binder/proc/stats文件 
        debugfs_create_file("stats",
                    0444,
                    binder_debugfs_dir_entry_root,
                    NULL,
                    &binder_stats_fops);
       // 创建binder/proc/transactions文件 
        debugfs_create_file("transactions",
                    0444,
                    binder_debugfs_dir_entry_root,
                    NULL,
                    &binder_transactions_fops);
       // 创建binder/proc/transaction_log文件 
        debugfs_create_file("transaction_log",
                    0444,
                    binder_debugfs_dir_entry_root,
                    &binder_transaction_log,
                    &binder_transaction_log_fops);
       // 创建binder/proc/failed_transaction_log文件 
        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) {
   
        device_names = kstrdup(binder_devices_param, GFP_KERNEL);
        // 创建binder设备
        device_tmp = device_names;
        while ((device_name = strsep(&device_tmp, ","))) {
   
            ret = init_binder_device(device_name);
        }
    }
    ret = init_binderfs();
    return ret;
}

打开binder驱动设备

static int binder_open(struct inode *nodp, struct file *filp)
{
   
    struct binder_proc *proc;
    struct binder_device *binder_dev;
    struct binderfs_info *info;
    struct dentry *binder_binderfs_dir_entry_proc = NULL;

    //  分配binder_proc结构体
    proc = kzalloc(sizeof(*proc), GFP_KERNEL);
    if (proc == NULL)
        return -ENOMEM;
    spin_lock_init(&proc->inner_lock);
    spin_lock_init(&proc->outer_lock);
    get_task_struct(current->group_leader);
    proc->tsk = current->group_leader;
    // 初始化proc->todo队列
    INIT_LIST_HEAD(&proc->todo);
    // 设置进程优先级
    proc->default_priority = task_nice(current);// 初始化context,它记录了ServiceManager信息,是全局静态变量
    proc->context = &binder_dev->context;
    binder_alloc_init(&proc->alloc);
    binder_stats_created(BINDER_STAT_PROC);
    proc->pid = current->group_leader->pid;
    INIT_LIST_HEAD(&proc->delivered_death);
    INIT_LIST_HEAD(&proc->waiting_threads);
    // 将proc保存到文件结构中,供下次调用使用
    filp->private_data = proc;

    mutex_lock(&binder_procs_lock);
    // 将proc添加到全局的binder_procs
    hlist_add_head(&proc->proc_node, &binder_procs);
    mutex_unlock(&binder_procs_lock);

    if (binder_debugfs_dir_entry_proc) {
   
        char strbuf[11];
        // 创建文件 /sys/kernel/debug/binde/proc/pid
        proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
            binder_debugfs_dir_entry_proc,
            (void *
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值