/*
 * livepatch_main.c - kernel live patch main infrastructure
 *
 * Copyright (c) 2014 SUSE
 *  Author: Miroslav Benes <mbenes@suse.cz>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/livepatch.h>
#include <linux/module.h>
#include <linux/types.h>

#include "uname_patch/livepatch_uname.h"

/* Auto expanded KLP_PATCHES_INCLUDES: */
#include "bsc1241579/livepatch_bsc1241579.h"
#include "bsc1242579/livepatch_bsc1242579.h"
#include "bsc1245218/livepatch_bsc1245218.h"
#include "bsc1245350/livepatch_bsc1245350.h"
#include "bsc1245505/livepatch_bsc1245505.h"
#include "bsc1245776/livepatch_bsc1245776.h"
#include "bsc1245791/livepatch_bsc1245791.h"
#include "bsc1245793/livepatch_bsc1245793.h"
#include "bsc1245797/livepatch_bsc1245797.h"
#include "bsc1246030/livepatch_bsc1246030.h"
#include "bsc1247351/livepatch_bsc1247351.h"


static struct klp_object objs[] = {
	/* Auto expanded KLP_PATCHES_OBJS: */
	{
		.name = NULL,
		.funcs = (struct klp_func[]) {
			{
			  .old_name = __stringify(KLP_SYSCALL_SYM(newuname)),
			  .new_func = KLP_SYSCALL_SYM(klp_newuname),
			},
#ifdef KLP_ARCH_HAS_SYSCALL_COMPAT_STUBS
			{
			  .old_name = __stringify(KLP_SYSCALL_COMPAT_STUB_SYM(newuname)),
			  .new_func = KLP_SYSCALL_COMPAT_STUB_SYM(klp_newuname),
			},
#endif
			{ .old_name = __stringify(proc_get_inode), .new_func = klpp_proc_get_inode, },
			{ .old_name = __stringify(proc_create_seq_private), .new_func = klpp_proc_create_seq_private, },
			{ .old_name = __stringify(proc_create_single_data), .new_func = klpp_proc_create_single_data, },
			{ .old_name = __stringify(pfifo_tail_enqueue), .new_func = klpp_pfifo_tail_enqueue, },
			{ .old_name = __stringify(shm_destroy_orphaned), .new_func = klpp_shm_destroy_orphaned, },
#if IS_ENABLED(CONFIG_HID)
			{ .old_name = __stringify(hid_alloc_report_buf), .new_func = klpp_hid_alloc_report_buf, },
			{ .old_name = __stringify(__hid_request), .new_func = klpp___hid_request, },
#endif
			{ }
		}
	},
	{
		.name = "algif_hash",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(hash_accept), .new_func = klpp_hash_accept, },
			{ }
		}
	},
	{
		.name = "btrfs",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(btrfs_create_pending_block_groups), .new_func = klpp_btrfs_create_pending_block_groups, },
			{ }
		}
	},
	{
		.name = "sch_hfsc",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(hfsc_enqueue), .new_func = klpp_hfsc_enqueue, },
			{ .old_name = __stringify(hfsc_change_class), .new_func = klpp_hfsc_change_class, },
			{ }
		}
	},
	{
		.name = "sch_prio",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(prio_tune), .new_func = klpp_prio_tune, },
			{ }
		}
	},
	{
		.name = "sch_sfq",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(sfq_init), .new_func = klpp_sfq_init, },
			{ }
		}
	},
	{
		.name = "sch_taprio",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(taprio_dev_notifier), .new_func = klpp_taprio_dev_notifier, },
			{ }
		}
	},
	{ }
};

static struct klp_patch patch = {
	.mod = THIS_MODULE,
	.objs = objs,
	.replace = true,
};

static int __init klp_patch_init(void)
{
	int retval;

	pr_info("livepatch: initializing\n");

	/* Auto expanded KLP_PATCHES_INIT_CALLS: */
	retval = livepatch_bsc1241579_init();
	if (retval)
		goto err_bsc1241579;

	retval = livepatch_bsc1242579_init();
	if (retval)
		goto err_bsc1242579;

	retval = livepatch_bsc1245218_init();
	if (retval)
		goto err_bsc1245218;

	retval = livepatch_bsc1245350_init();
	if (retval)
		goto err_bsc1245350;

	retval = livepatch_bsc1245505_init();
	if (retval)
		goto err_bsc1245505;

	retval = livepatch_bsc1245776_init();
	if (retval)
		goto err_bsc1245776;

	retval = livepatch_bsc1245791_init();
	if (retval)
		goto err_bsc1245791;

	retval = livepatch_bsc1245793_init();
	if (retval)
		goto err_bsc1245793;

	retval = livepatch_bsc1245797_init();
	if (retval)
		goto err_bsc1245797;

	retval = livepatch_bsc1246030_init();
	if (retval)
		goto err_bsc1246030;

	retval = livepatch_bsc1247351_init();
	if (retval)
		goto err_bsc1247351;

	retval = klp_enable_patch(&patch);
	if (!retval)
		return retval;

	/* Auto expanded KLP_PATCHES_INIT_ERR_HANDLERS: */
	livepatch_bsc1247351_cleanup();
err_bsc1247351:
	livepatch_bsc1246030_cleanup();
err_bsc1246030:
	livepatch_bsc1245797_cleanup();
err_bsc1245797:
	livepatch_bsc1245793_cleanup();
err_bsc1245793:
	livepatch_bsc1245791_cleanup();
err_bsc1245791:
	livepatch_bsc1245776_cleanup();
err_bsc1245776:
	livepatch_bsc1245505_cleanup();
err_bsc1245505:
	livepatch_bsc1245350_cleanup();
err_bsc1245350:
	livepatch_bsc1245218_cleanup();
err_bsc1245218:
	livepatch_bsc1242579_cleanup();
err_bsc1242579:
	livepatch_bsc1241579_cleanup();
err_bsc1241579:

	return retval;
}

static void __exit klp_patch_cleanup(void)
{
	pr_info("livepatch: removed\n");

	/* Auto expanded KLP_PATCHES_CLEANUP_CALLS: */
	livepatch_bsc1241579_cleanup();
	livepatch_bsc1242579_cleanup();
	livepatch_bsc1245218_cleanup();
	livepatch_bsc1245350_cleanup();
	livepatch_bsc1245505_cleanup();
	livepatch_bsc1245776_cleanup();
	livepatch_bsc1245791_cleanup();
	livepatch_bsc1245793_cleanup();
	livepatch_bsc1245797_cleanup();
	livepatch_bsc1246030_cleanup();
	livepatch_bsc1247351_cleanup();

}

module_init(klp_patch_init);
module_exit(klp_patch_cleanup);

MODULE_LICENSE("GPL");
MODULE_INFO(livepatch, "Y");
MODULE_INFO(klpgitrev, "9c31fb8f24c3704e1e6450fae29ef56cc102d049");
