/*
 * 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 "bsc1270023/livepatch_bsc1270023.h"
#include "bsc1271543/livepatch_bsc1271543.h"
#include "bsc1271867/livepatch_bsc1271867.h"
#include "bsc1273232/livepatch_bsc1273232.h"
#include "bsc1274074/livepatch_bsc1274074.h"
#include "bsc1274942/livepatch_bsc1274942.h"
#include "bsc1275458/livepatch_bsc1275458.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(tcf_qevent_handle), .new_func = klpp_tcf_qevent_handle, },
			{ .old_name = __stringify(qdisc_get_rtab), .new_func = klpp_qdisc_get_rtab, },
			{ .old_name = __stringify(qdisc_put_rtab), .new_func = klpp_qdisc_put_rtab, },
			{ .old_name = __stringify(ip_mc_destroy_dev), .new_func = klpp_ip_mc_destroy_dev, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_X86)
	{
		.name = "kvm",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(direct_page_fault), .new_func = klpp_direct_page_fault, },
			{ .old_name = __stringify(ept_page_fault), .new_func = klpp_ept_page_fault, },
			{ .old_name = __stringify(paging32_page_fault), .new_func = klpp_paging32_page_fault, },
			{ .old_name = __stringify(paging64_page_fault), .new_func = klpp_paging64_page_fault, },
			{ }
		}
	},
#endif
	{
		.name = "sctp",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(sctp_unpack_cookie), .new_func = klpp_sctp_unpack_cookie, },
			{ .old_name = __stringify(sctp_raw_to_bind_addrs), .new_func = klpp_sctp_raw_to_bind_addrs, },
			{ .old_name = __stringify(sctp_process_asconf), .new_func = klpp_sctp_process_asconf, },
			{ }
		}
	},
	{
		.name = "xfs",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(xfs_reflink_allocate_cow), .new_func = klpp_xfs_reflink_allocate_cow, },
			{ }
		}
	},
	{ }
};

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_bsc1270023_init();
	if (retval)
		goto err_bsc1270023;

	retval = livepatch_bsc1271543_init();
	if (retval)
		goto err_bsc1271543;

	retval = livepatch_bsc1271867_init();
	if (retval)
		goto err_bsc1271867;

	retval = livepatch_bsc1273232_init();
	if (retval)
		goto err_bsc1273232;

	retval = livepatch_bsc1274074_init();
	if (retval)
		goto err_bsc1274074;

	retval = livepatch_bsc1274942_init();
	if (retval)
		goto err_bsc1274942;

	retval = livepatch_bsc1275458_init();
	if (retval)
		goto err_bsc1275458;

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

	/* Auto expanded KLP_PATCHES_INIT_ERR_HANDLERS: */
	livepatch_bsc1275458_cleanup();
err_bsc1275458:
	livepatch_bsc1274942_cleanup();
err_bsc1274942:
	livepatch_bsc1274074_cleanup();
err_bsc1274074:
	livepatch_bsc1273232_cleanup();
err_bsc1273232:
	livepatch_bsc1271867_cleanup();
err_bsc1271867:
	livepatch_bsc1271543_cleanup();
err_bsc1271543:
	livepatch_bsc1270023_cleanup();
err_bsc1270023:

	return retval;
}

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

	/* Auto expanded KLP_PATCHES_CLEANUP_CALLS: */
	livepatch_bsc1270023_cleanup();
	livepatch_bsc1271543_cleanup();
	livepatch_bsc1271867_cleanup();
	livepatch_bsc1273232_cleanup();
	livepatch_bsc1274074_cleanup();
	livepatch_bsc1274942_cleanup();
	livepatch_bsc1275458_cleanup();

}

module_init(klp_patch_init);
module_exit(klp_patch_cleanup);

MODULE_LICENSE("GPL");
MODULE_INFO(livepatch, "Y");
MODULE_INFO(klpgitrev, "0f3f60ef8a10282f1b4c3d0be195a0d08afb3a56");
