/*
 * 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 "bsc1251982/livepatch_bsc1251982.h"
#include "bsc1253415/livepatch_bsc1253415.h"
#include "bsc1253437/livepatch_bsc1253437.h"
#include "bsc1253439/livepatch_bsc1253439.h"
#include "bsc1254196/livepatch_bsc1254196.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(io_link_skb), .new_func = klpp_io_link_skb, },
			{ .old_name = __stringify(tcp_conn_request), .new_func = klpp_tcp_conn_request, },
			{ }
		}
	},
	{
		.name = "nfsd",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(__fh_verify), .new_func = klpp___fh_verify, },
			{ }
		}
	},
	{
		.name = "sctp",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(sctp_unpack_cookie), .new_func = klpp_sctp_unpack_cookie, },
			{ .old_name = __stringify(sctp_sf_authenticate), .new_func = klpp_sctp_sf_authenticate, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_SCSI_UFSHCD)
	{
		.name = "ufshcd_core",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(pm_qos_enable_show), .new_func = klpp_pm_qos_enable_show, },
			{ .old_name = __stringify(ufshcd_pm_qos_init), .new_func = klpp_ufshcd_pm_qos_init, },
			{ .old_name = __stringify(ufshcd_pm_qos_exit), .new_func = klpp_ufshcd_pm_qos_exit, },
			{ .old_name = __stringify(ufshcd_scale_clks), .new_func = klpp_ufshcd_scale_clks, },
			{ .old_name = __stringify(ufshcd_setup_clocks), .new_func = klpp_ufshcd_setup_clocks, },
			{ .old_name = __stringify(ufshcd_suspend), .new_func = klpp_ufshcd_suspend, },
			{ .old_name = __stringify(ufshcd_init), .new_func = klpp_ufshcd_init, },
			{ .old_name = __stringify(ufshcd_remove), .new_func = klpp_ufshcd_remove, },
			{ }
		}
	},
#endif
	{ }
};

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_bsc1251982_init();
	if (retval)
		goto err_bsc1251982;

	retval = livepatch_bsc1253415_init();
	if (retval)
		goto err_bsc1253415;

	retval = livepatch_bsc1253437_init();
	if (retval)
		goto err_bsc1253437;

	retval = livepatch_bsc1253439_init();
	if (retval)
		goto err_bsc1253439;

	retval = livepatch_bsc1254196_init();
	if (retval)
		goto err_bsc1254196;

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

	/* Auto expanded KLP_PATCHES_INIT_ERR_HANDLERS: */
	livepatch_bsc1254196_cleanup();
err_bsc1254196:
	livepatch_bsc1253439_cleanup();
err_bsc1253439:
	livepatch_bsc1253437_cleanup();
err_bsc1253437:
	livepatch_bsc1253415_cleanup();
err_bsc1253415:
	livepatch_bsc1251982_cleanup();
err_bsc1251982:

	return retval;
}

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

	/* Auto expanded KLP_PATCHES_CLEANUP_CALLS: */
	livepatch_bsc1251982_cleanup();
	livepatch_bsc1253415_cleanup();
	livepatch_bsc1253437_cleanup();
	livepatch_bsc1253439_cleanup();
	livepatch_bsc1254196_cleanup();

}

module_init(klp_patch_init);
module_exit(klp_patch_cleanup);

MODULE_LICENSE("GPL");
MODULE_INFO(livepatch, "Y");
MODULE_INFO(klpgitrev, "5ff9752e894bf46f29a1564acca742ba1f3f9f8a");
