/*
 * 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 "bsc1250280/livepatch_bsc1250280.h"
#include "bsc1250785/livepatch_bsc1250785.h"
#include "bsc1251787/livepatch_bsc1251787.h"
#include "bsc1252563/livepatch_bsc1252563.h"
#include "bsc1253437/livepatch_bsc1253437.h"
#include "bsc1253439/livepatch_bsc1253439.h"
#include "bsc1254451/livepatch_bsc1254451.h"
#include "bsc1254755/livepatch_bsc1254755.h"
#include "bsc1255402/livepatch_bsc1255402.h"
#include "bsc1255577/livepatch_bsc1255577.h"
#include "bsc1255595/livepatch_bsc1255595.h"
#include "bsc1255845/livepatch_bsc1255845.h"
#include "bsc1256217/livepatch_bsc1256217.h"
#include "bsc1256624/livepatch_bsc1256624.h"
#include "bsc1256644/livepatch_bsc1256644.h"
#include "bsc1257118/livepatch_bsc1257118.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(inode_init_always), .new_func = klpp_inode_init_always, },
			{ .old_name = __stringify(module_memfree), .new_func = klpp_module_memfree, },
#if IS_ENABLED(CONFIG_ACPI)
			{ .old_name = __stringify(acpi_ut_copy_iobject_to_iobject), .new_func = klpp_acpi_ut_copy_iobject_to_iobject, },
#endif
			{ .old_name = __stringify(tcp_conn_request), .new_func = klpp_tcp_conn_request, },
#if IS_ENABLED(CONFIG_ACPI_PROCESSOR)
			{ .old_name = __stringify(acpi_processor_get_lpi_info), .new_func = klpp_acpi_processor_get_lpi_info, },
#endif
			{ .old_name = __stringify(calipso_skbuff_setattr), .new_func = klpp_calipso_skbuff_setattr, .old_sympos = 1, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_ATH10K)
	{
		.name = "ath10k_core",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(ath10k_htc_notify_tx_completion), .new_func = klpp_ath10k_htc_notify_tx_completion, },
			{ }
		}
	},
#endif
#if IS_ENABLED(CONFIG_ATH9K_HTC)
	{
		.name = "ath9k_htc",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(ath9k_wmi_cmd), .new_func = klpp_ath9k_wmi_cmd, },
			{ }
		}
	},
#endif
#if (!IS_ENABLED(CONFIG_S390)) || (IS_ENABLED(CONFIG_S390))
	{
		.name = "ip_vs",
		.funcs = (struct klp_func[]) {
#if !IS_ENABLED(CONFIG_S390)
			{ .old_name = __stringify(__ip_vs_get_out_rt), .new_func = klpp___ip_vs_get_out_rt, },
#endif
#if IS_ENABLED(CONFIG_S390)
			{ .old_name = __stringify(ip_vs_bypass_xmit), .new_func = klpp_ip_vs_bypass_xmit, },
			{ .old_name = __stringify(ip_vs_dr_xmit), .new_func = klpp_ip_vs_dr_xmit, },
			{ .old_name = __stringify(ip_vs_icmp_xmit), .new_func = klpp_ip_vs_icmp_xmit, },
			{ .old_name = __stringify(ip_vs_nat_xmit), .new_func = klpp_ip_vs_nat_xmit, },
			{ .old_name = __stringify(ip_vs_tunnel_xmit), .new_func = klpp_ip_vs_tunnel_xmit, },
#endif
			{ }
		}
	},
#endif
	{
		.name = "iscsi_target_mod",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(lio_target_nacl_info_show), .new_func = klpp_lio_target_nacl_info_show, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_ATA)
	{
		.name = "libata",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(ata_pio_sector), .new_func = klpp_ata_pio_sector, },
			{ }
		}
	},
#endif
	{
		.name = "libceph",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(__ceph_open_session), .new_func = klpp___ceph_open_session, },
			{ .old_name = __stringify(monmap_show), .new_func = klpp_monmap_show, },
			{ .old_name = __stringify(osdmap_show), .new_func = klpp_osdmap_show, },
			{ }
		}
	},
	{
		.name = "mrp",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(mrp_uninit_applicant), .new_func = klpp_mrp_uninit_applicant, },
			{ }
		}
	},
	{
		.name = "nvme",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(nvme_probe), .new_func = klpp_nvme_probe, },
			{ .old_name = __stringify(nvme_reset_work), .new_func = klpp_nvme_reset_work, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_NVME_TARGET_TCP)
	{
		.name = "nvmet_tcp",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(nvmet_tcp_try_recv_pdu), .new_func = klpp_nvmet_tcp_try_recv_pdu, },
			{ }
		}
	},
#endif
	{
		.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_SMC)
	{
		.name = "smc",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(smc_create), .new_func = klpp_smc_create, },
			{ }
		}
	},
#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");

	retval = klp_patch_uname_init();
	if (retval)
		return retval;

	/* Auto expanded KLP_PATCHES_INIT_CALLS: */
	retval = livepatch_bsc1250280_init();
	if (retval)
		goto err_bsc1250280;

	retval = livepatch_bsc1250785_init();
	if (retval)
		goto err_bsc1250785;

	retval = livepatch_bsc1251787_init();
	if (retval)
		goto err_bsc1251787;

	retval = livepatch_bsc1252563_init();
	if (retval)
		goto err_bsc1252563;

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

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

	retval = livepatch_bsc1254451_init();
	if (retval)
		goto err_bsc1254451;

	retval = livepatch_bsc1254755_init();
	if (retval)
		goto err_bsc1254755;

	retval = livepatch_bsc1255402_init();
	if (retval)
		goto err_bsc1255402;

	retval = livepatch_bsc1255577_init();
	if (retval)
		goto err_bsc1255577;

	retval = livepatch_bsc1255595_init();
	if (retval)
		goto err_bsc1255595;

	retval = livepatch_bsc1255845_init();
	if (retval)
		goto err_bsc1255845;

	retval = livepatch_bsc1256217_init();
	if (retval)
		goto err_bsc1256217;

	retval = livepatch_bsc1256624_init();
	if (retval)
		goto err_bsc1256624;

	retval = livepatch_bsc1256644_init();
	if (retval)
		goto err_bsc1256644;

	retval = livepatch_bsc1257118_init();
	if (retval)
		goto err_bsc1257118;

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

	/* Auto expanded KLP_PATCHES_INIT_ERR_HANDLERS: */
	livepatch_bsc1257118_cleanup();
err_bsc1257118:
	livepatch_bsc1256644_cleanup();
err_bsc1256644:
	livepatch_bsc1256624_cleanup();
err_bsc1256624:
	livepatch_bsc1256217_cleanup();
err_bsc1256217:
	livepatch_bsc1255845_cleanup();
err_bsc1255845:
	livepatch_bsc1255595_cleanup();
err_bsc1255595:
	livepatch_bsc1255577_cleanup();
err_bsc1255577:
	livepatch_bsc1255402_cleanup();
err_bsc1255402:
	livepatch_bsc1254755_cleanup();
err_bsc1254755:
	livepatch_bsc1254451_cleanup();
err_bsc1254451:
	livepatch_bsc1253439_cleanup();
err_bsc1253439:
	livepatch_bsc1253437_cleanup();
err_bsc1253437:
	livepatch_bsc1252563_cleanup();
err_bsc1252563:
	livepatch_bsc1251787_cleanup();
err_bsc1251787:
	livepatch_bsc1250785_cleanup();
err_bsc1250785:
	livepatch_bsc1250280_cleanup();
err_bsc1250280:

	return retval;
}

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

	/* Auto expanded KLP_PATCHES_CLEANUP_CALLS: */
	livepatch_bsc1250280_cleanup();
	livepatch_bsc1250785_cleanup();
	livepatch_bsc1251787_cleanup();
	livepatch_bsc1252563_cleanup();
	livepatch_bsc1253437_cleanup();
	livepatch_bsc1253439_cleanup();
	livepatch_bsc1254451_cleanup();
	livepatch_bsc1254755_cleanup();
	livepatch_bsc1255402_cleanup();
	livepatch_bsc1255577_cleanup();
	livepatch_bsc1255595_cleanup();
	livepatch_bsc1255845_cleanup();
	livepatch_bsc1256217_cleanup();
	livepatch_bsc1256624_cleanup();
	livepatch_bsc1256644_cleanup();
	livepatch_bsc1257118_cleanup();

}

module_init(klp_patch_init);
module_exit(klp_patch_cleanup);

MODULE_LICENSE("GPL");
MODULE_INFO(livepatch, "Y");
