linux-stable/kernel/regset.c

91 lines
2.2 KiB
C
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0-only
#include <linux/export.h>
#include <linux/slab.h>
#include <linux/regset.h>
static int __regset_get(struct task_struct *target,
const struct user_regset *regset,
unsigned int size,
void **data)
{
void *p = *data, *to_free = NULL;
int res;
regset: new method and helpers for it ->regset_get() takes task+regset+buffer, returns the amount of free space left in the buffer on success and -E... on error. buffer is represented as struct membuf - a pair of (kernel) pointer and amount of space left Primitives for writing to such: * membuf_write(buf, data, size) * membuf_zero(buf, size) * membuf_store(buf, value) These are implemented as inlines (in case of membuf_store - a macro). All writes are sequential; they become no-ops when there's no space left. Return value of all primitives is the amount of space left after the operation, so they can be used as return values of ->regset_get(). Example of use: // stores pt_regs of task + 64 bytes worth of zeroes + 32bit PID of task int foo_get(struct task_struct *task, const struct regset *regset, struct membuf to) { membuf_write(&to, task_pt_regs(task), sizeof(struct pt_regs)); membuf_zero(&to, 64); return membuf_store(&to, (u32)task_tgid_vnr(task)); } regset_get()/regset_get_alloc() taught to use that thing if present. By the end of the series all users of ->get() will be converted; then ->get() and ->get_size() can go. Note that unlike ->get() this thing always starts at offset 0 and, since it only writes to kernel buffer, can't fail on copyout. It can, of course, fail for other reasons, but those tend to be less numerous. The caller guarantees that the buffer size won't be bigger than regset->n * regset->size. That simplifies life for quite a few instances. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
2020-02-21 01:48:16 +00:00
if (!regset->get && !regset->regset_get)
return -EOPNOTSUPP;
if (size > regset->n * regset->size)
size = regset->n * regset->size;
if (!p) {
to_free = p = kzalloc(size, GFP_KERNEL);
if (!p)
return -ENOMEM;
}
regset: new method and helpers for it ->regset_get() takes task+regset+buffer, returns the amount of free space left in the buffer on success and -E... on error. buffer is represented as struct membuf - a pair of (kernel) pointer and amount of space left Primitives for writing to such: * membuf_write(buf, data, size) * membuf_zero(buf, size) * membuf_store(buf, value) These are implemented as inlines (in case of membuf_store - a macro). All writes are sequential; they become no-ops when there's no space left. Return value of all primitives is the amount of space left after the operation, so they can be used as return values of ->regset_get(). Example of use: // stores pt_regs of task + 64 bytes worth of zeroes + 32bit PID of task int foo_get(struct task_struct *task, const struct regset *regset, struct membuf to) { membuf_write(&to, task_pt_regs(task), sizeof(struct pt_regs)); membuf_zero(&to, 64); return membuf_store(&to, (u32)task_tgid_vnr(task)); } regset_get()/regset_get_alloc() taught to use that thing if present. By the end of the series all users of ->get() will be converted; then ->get() and ->get_size() can go. Note that unlike ->get() this thing always starts at offset 0 and, since it only writes to kernel buffer, can't fail on copyout. It can, of course, fail for other reasons, but those tend to be less numerous. The caller guarantees that the buffer size won't be bigger than regset->n * regset->size. That simplifies life for quite a few instances. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
2020-02-21 01:48:16 +00:00
if (regset->regset_get) {
res = regset->regset_get(target, regset,
(struct membuf){.p = p, .left = size});
if (res < 0) {
kfree(to_free);
return res;
}
*data = p;
return size - res;
}
res = regset->get(target, regset, 0, size, p, NULL);
if (unlikely(res < 0)) {
kfree(to_free);
return res;
}
*data = p;
if (regset->get_size) { // arm64-only kludge, will go away
unsigned max_size = regset->get_size(target, regset);
if (size > max_size)
size = max_size;
}
return size;
}
int regset_get(struct task_struct *target,
const struct user_regset *regset,
unsigned int size,
void *data)
{
return __regset_get(target, regset, size, &data);
}
EXPORT_SYMBOL(regset_get);
int regset_get_alloc(struct task_struct *target,
const struct user_regset *regset,
unsigned int size,
void **data)
{
*data = NULL;
return __regset_get(target, regset, size, data);
}
EXPORT_SYMBOL(regset_get_alloc);
/**
* copy_regset_to_user - fetch a thread's user_regset data into user memory
* @target: thread to be examined
* @view: &struct user_regset_view describing user thread machine state
* @setno: index in @view->regsets
* @offset: offset into the regset data, in bytes
* @size: amount of data to copy, in bytes
* @data: user-mode pointer to copy into
*/
int copy_regset_to_user(struct task_struct *target,
const struct user_regset_view *view,
unsigned int setno,
unsigned int offset, unsigned int size,
void __user *data)
{
const struct user_regset *regset = &view->regsets[setno];
void *buf;
int ret;
ret = regset_get_alloc(target, regset, size, &buf);
if (ret > 0)
ret = copy_to_user(data, buf, ret) ? -EFAULT : 0;
kfree(buf);
return ret;
}