aboutsummaryrefslogblamecommitdiffstats
path: root/drivers/cpufreq/speedstep-centrino.c
blob: 6ea3455def2165e970ee3e35e6910f2598af8218 (plain) (tree)
1
2
3
4
5
6
7



                                                                     


                                                                     





                                                                      





                          
                                             

                           
                      
 



                           
                                              
                                                 
 
                                












                                                

                     






                                        

                                       
  
                                   








                                                                            

                                                              

                                      

                                                           




















































































































































                                                                          

                                                  







                                                                 
                                                         









                                                                        
                                                            






                                                                
                                                                   
                                          
                                                                


                               
                                                     
 
                                                        





                                                   



                                                                        

                                                

                                                              

















                                                                           


                                                                      



                                        

                                                       


                         






                                                                               

                     
                                                                              








                                                  
 
                                                        








                                                                           
                                                             

                                                      



                          

                                                           
                                                         





                                                              

                                                  

                               
                                                   
                                                             
 

                               
 


                                                             
 
                       
                                                                 
 
                                                  
                                                      



                                                            
 

                                              





                                                                    

                                                             
                                                                          



                                                  
                                                                     

                                                                        




                                         

                                                                             

                           
                                                                
 

                                                                 


                             

                                                                              







                                                           
                                          



                                              
                                            












                                                                 

                                                                         





                                                

                                                              







                                                          
                                                                  
                                      

                                                     
                                   
 
                                                                     
                               
 
                                                             


                                 
 
                                                           
                                                                


                                      

                                 

         
                      
                                       
                             




                                                                         



                                                                            
                                                                   

                                                                    
                    
                                     
 
                                             
                                                                            


                                                                            
                                         


                              
 
                                                                              

                                
                                                                               
                                                       
                                                                                

                                                              
                                         




                                                                  
                                                                         

                                                                        
                                                       

                                                   










                                                                               
 

                                                                     
                              
 
                                                 
         
 
                                       

                                   


                                                                      
 






                                                                       
 

                                                                      
 


                                      
                                               

                                           



                                                                              
                   
 
    
                                       
                      



































                                                                       
                                               

















                                                                                 
/*
 * cpufreq driver for Enhanced SpeedStep, as found in Intel's Pentium
 * M (part of the Centrino chipset).
 *
 * Since the original Pentium M, most new Intel CPUs support Enhanced
 * SpeedStep.
 *
 * Despite the "SpeedStep" in the name, this is almost entirely unlike
 * traditional SpeedStep.
 *
 * Modelled on speedstep.c
 *
 * Copyright (C) 2003 Jeremy Fitzhardinge <jeremy@goop.org>
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/cpufreq.h>
#include <linux/sched.h>	/* current */
#include <linux/delay.h>
#include <linux/compiler.h>
#include <linux/gfp.h>

#include <asm/msr.h>
#include <asm/processor.h>
#include <asm/cpufeature.h>

#define PFX		"speedstep-centrino: "
#define MAINTAINER	"cpufreq@vger.kernel.org"

#define INTEL_MSR_RANGE	(0xffff)

struct cpu_id
{
	__u8	x86;            /* CPU family */
	__u8	x86_model;	/* model */
	__u8	x86_mask;	/* stepping */
};

enum {
	CPU_BANIAS,
	CPU_DOTHAN_A1,
	CPU_DOTHAN_A2,
	CPU_DOTHAN_B0,
	CPU_MP4HT_D0,
	CPU_MP4HT_E0,
};

static const struct cpu_id cpu_ids[] = {
	[CPU_BANIAS]	= { 6,  9, 5 },
	[CPU_DOTHAN_A1]	= { 6, 13, 1 },
	[CPU_DOTHAN_A2]	= { 6, 13, 2 },
	[CPU_DOTHAN_B0]	= { 6, 13, 6 },
	[CPU_MP4HT_D0]	= {15,  3, 4 },
	[CPU_MP4HT_E0]	= {15,  4, 1 },
};
#define N_IDS	ARRAY_SIZE(cpu_ids)

struct cpu_model
{
	const struct cpu_id *cpu_id;
	const char	*model_name;
	unsigned	max_freq; /* max clock in kHz */

	struct cpufreq_frequency_table *op_points; /* clock/voltage pairs */
};
static int centrino_verify_cpu_id(const struct cpuinfo_x86 *c,
				  const struct cpu_id *x);

/* Operating points for current CPU */
static DEFINE_PER_CPU(struct cpu_model *, centrino_model);
static DEFINE_PER_CPU(const struct cpu_id *, centrino_cpu);

static struct cpufreq_driver centrino_driver;

#ifdef CONFIG_X86_SPEEDSTEP_CENTRINO_TABLE

/* Computes the correct form for IA32_PERF_CTL MSR for a particular
   frequency/voltage operating point; frequency in MHz, volts in mV.
   This is stored as "index" in the structure. */
#define OP(mhz, mv)							\
	{								\
		.frequency = (mhz) * 1000,				\
		.index = (((mhz)/100) << 8) | ((mv - 700) / 16)		\
	}

/*
 * These voltage tables were derived from the Intel Pentium M
 * datasheet, document 25261202.pdf, Table 5.  I have verified they
 * are consistent with my IBM ThinkPad X31, which has a 1.3GHz Pentium
 * M.
 */

/* Ultra Low Voltage Intel Pentium M processor 900MHz (Banias) */
static struct cpufreq_frequency_table banias_900[] =
{
	OP(600,  844),
	OP(800,  988),
	OP(900, 1004),
	{ .frequency = CPUFREQ_TABLE_END }
};

/* Ultra Low Voltage Intel Pentium M processor 1000MHz (Banias) */
static struct cpufreq_frequency_table banias_1000[] =
{
	OP(600,   844),
	OP(800,   972),
	OP(900,   988),
	OP(1000, 1004),
	{ .frequency = CPUFREQ_TABLE_END }
};

/* Low Voltage Intel Pentium M processor 1.10GHz (Banias) */
static struct cpufreq_frequency_table banias_1100[] =
{
	OP( 600,  956),
	OP( 800, 1020),
	OP( 900, 1100),
	OP(1000, 1164),
	OP(1100, 1180),
	{ .frequency = CPUFREQ_TABLE_END }
};


/* Low Voltage Intel Pentium M processor 1.20GHz (Banias) */
static struct cpufreq_frequency_table banias_1200[] =
{
	OP( 600,  956),
	OP( 800, 1004),
	OP( 900, 1020),
	OP(1000, 1100),
	OP(1100, 1164),
	OP(1200, 1180),
	{ .frequency = CPUFREQ_TABLE_END }
};

/* Intel Pentium M processor 1.30GHz (Banias) */
static struct cpufreq_frequency_table banias_1300[] =
{
	OP( 600,  956),
	OP( 800, 1260),
	OP(1000, 1292),
	OP(1200, 1356),
	OP(1300, 1388),
	{ .frequency = CPUFREQ_TABLE_END }
};

/* Intel Pentium M processor 1.40GHz (Banias) */
static struct cpufreq_frequency_table banias_1400[] =
{
	OP( 600,  956),
	OP( 800, 1180),
	OP(1000, 1308),
	OP(1200, 1436),
	OP(1400, 1484),
	{ .frequency = CPUFREQ_TABLE_END }
};

/* Intel Pentium M processor 1.50GHz (Banias) */
static struct cpufreq_frequency_table banias_1500[] =
{
	OP( 600,  956),
	OP( 800, 1116),
	OP(1000, 1228),
	OP(1200, 1356),
	OP(1400, 1452),
	OP(1500, 1484),
	{ .frequency = CPUFREQ_TABLE_END }
};

/* Intel Pentium M processor 1.60GHz (Banias) */
static struct cpufreq_frequency_table banias_1600[] =
{
	OP( 600,  956),
	OP( 800, 1036),
	OP(1000, 1164),
	OP(1200, 1276),
	OP(1400, 1420),
	OP(1600, 1484),
	{ .frequency = CPUFREQ_TABLE_END }
};

/* Intel Pentium M processor 1.70GHz (Banias) */
static struct cpufreq_frequency_table banias_1700[] =
{
	OP( 600,  956),
	OP( 800, 1004),
	OP(1000, 1116),
	OP(1200, 1228),
	OP(1400, 1308),
	OP(1700, 1484),
	{ .frequency = CPUFREQ_TABLE_END }
};
#undef OP

#define _BANIAS(cpuid, max, name)	\
{	.cpu_id		= cpuid,	\
	.model_name	= "Intel(R) Pentium(R) M processor " name "MHz", \
	.max_freq	= (max)*1000,	\
	.op_points	= banias_##max,	\
}
#define BANIAS(max)	_BANIAS(&cpu_ids[CPU_BANIAS], max, #max)

/* CPU models, their operating frequency range, and freq/voltage
   operating points */
static struct cpu_model models[] =
{
	_BANIAS(&cpu_ids[CPU_BANIAS], 900, " 900"),
	BANIAS(1000),
	BANIAS(1100),
	BANIAS(1200),
	BANIAS(1300),
	BANIAS(1400),
	BANIAS(1500),
	BANIAS(1600),
	BANIAS(1700),

	/* NULL model_name is a wildcard */
	{ &cpu_ids[CPU_DOTHAN_A1], NULL, 0, NULL },
	{ &cpu_ids[CPU_DOTHAN_A2], NULL, 0, NULL },
	{ &cpu_ids[CPU_DOTHAN_B0], NULL, 0, NULL },
	{ &cpu_ids[CPU_MP4HT_D0], NULL, 0, NULL },
	{ &cpu_ids[CPU_MP4HT_E0], NULL, 0, NULL },

	{ NULL, }
};
#undef _BANIAS
#undef BANIAS

static int centrino_cpu_init_table(struct cpufreq_policy *policy)
{
	struct cpuinfo_x86 *cpu = &cpu_data(policy->cpu);
	struct cpu_model *model;

	for(model = models; model->cpu_id != NULL; model++)
		if (centrino_verify_cpu_id(cpu, model->cpu_id) &&
		    (model->model_name == NULL ||
		     strcmp(cpu->x86_model_id, model->model_name) == 0))
			break;

	if (model->cpu_id == NULL) {
		/* No match at all */
		pr_debug("no support for CPU model \"%s\": "
		       "send /proc/cpuinfo to " MAINTAINER "\n",
		       cpu->x86_model_id);
		return -ENOENT;
	}

	if (model->op_points == NULL) {
		/* Matched a non-match */
		pr_debug("no table support for CPU model \"%s\"\n",
		       cpu->x86_model_id);
		pr_debug("try using the acpi-cpufreq driver\n");
		return -ENOENT;
	}

	per_cpu(centrino_model, policy->cpu) = model;

	pr_debug("found \"%s\": max frequency: %dkHz\n",
	       model->model_name, model->max_freq);

	return 0;
}

#else
static inline int centrino_cpu_init_table(struct cpufreq_policy *policy)
{
	return -ENODEV;
}
#endif /* CONFIG_X86_SPEEDSTEP_CENTRINO_TABLE */

static int centrino_verify_cpu_id(const struct cpuinfo_x86 *c,
				  const struct cpu_id *x)
{
	if ((c->x86 == x->x86) &&
	    (c->x86_model == x->x86_model) &&
	    (c->x86_mask == x->x86_mask))
		return 1;
	return 0;
}

/* To be called only after centrino_model is initialized */
static unsigned extract_clock(unsigned msr, unsigned int cpu, int failsafe)
{
	int i;

	/*
	 * Extract clock in kHz from PERF_CTL value
	 * for centrino, as some DSDTs are buggy.
	 * Ideally, this can be done using the acpi_data structure.
	 */
	if ((per_cpu(centrino_cpu, cpu) == &cpu_ids[CPU_BANIAS]) ||
	    (per_cpu(centrino_cpu, cpu) == &cpu_ids[CPU_DOTHAN_A1]) ||
	    (per_cpu(centrino_cpu, cpu) == &cpu_ids[CPU_DOTHAN_B0])) {
		msr = (msr >> 8) & 0xff;
		return msr * 100000;
	}

	if ((!per_cpu(centrino_model, cpu)) ||
	    (!per_cpu(centrino_model, cpu)->op_points))
		return 0;

	msr &= 0xffff;
	for (i = 0;
		per_cpu(centrino_model, cpu)->op_points[i].frequency
							!= CPUFREQ_TABLE_END;
	     i++) {
		if (msr == per_cpu(centrino_model, cpu)->op_points[i].index)
			return per_cpu(centrino_model, cpu)->
							op_points[i].frequency;
	}
	if (failsafe)
		return per_cpu(centrino_model, cpu)->op_points[i-1].frequency;
	else
		return 0;
}

/* Return the current CPU frequency in kHz */
static unsigned int get_cur_freq(unsigned int cpu)
{
	unsigned l, h;
	unsigned clock_freq;

	rdmsr_on_cpu(cpu, MSR_IA32_PERF_STATUS, &l, &h);
	clock_freq = extract_clock(l, cpu, 0);

	if (unlikely(clock_freq == 0)) {
		/*
		 * On some CPUs, we can see transient MSR values (which are
		 * not present in _PSS), while CPU is doing some automatic
		 * P-state transition (like TM2). Get the last freq set 
		 * in PERF_CTL.
		 */
		rdmsr_on_cpu(cpu, MSR_IA32_PERF_CTL, &l, &h);
		clock_freq = extract_clock(l, cpu, 1);
	}
	return clock_freq;
}


static int centrino_cpu_init(struct cpufreq_policy *policy)
{
	struct cpuinfo_x86 *cpu = &cpu_data(policy->cpu);
	unsigned freq;
	unsigned l, h;
	int ret;
	int i;

	/* Only Intel makes Enhanced Speedstep-capable CPUs */
	if (cpu->x86_vendor != X86_VENDOR_INTEL ||
	    !cpu_has(cpu, X86_FEATURE_EST))
		return -ENODEV;

	if (cpu_has(cpu, X86_FEATURE_CONSTANT_TSC))
		centrino_driver.flags |= CPUFREQ_CONST_LOOPS;

	if (policy->cpu != 0)
		return -ENODEV;

	for (i = 0; i < N_IDS; i++)
		if (centrino_verify_cpu_id(cpu, &cpu_ids[i]))
			break;

	if (i != N_IDS)
		per_cpu(centrino_cpu, policy->cpu) = &cpu_ids[i];

	if (!per_cpu(centrino_cpu, policy->cpu)) {
		pr_debug("found unsupported CPU with "
		"Enhanced SpeedStep: send /proc/cpuinfo to "
		MAINTAINER "\n");
		return -ENODEV;
	}

	if (centrino_cpu_init_table(policy)) {
		return -ENODEV;
	}

	/* Check to see if Enhanced SpeedStep is enabled, and try to
	   enable it if not. */
	rdmsr(MSR_IA32_MISC_ENABLE, l, h);

	if (!(l & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP)) {
		l |= MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP;
		pr_debug("trying to enable Enhanced SpeedStep (%x)\n", l);
		wrmsr(MSR_IA32_MISC_ENABLE, l, h);

		/* check to see if it stuck */
		rdmsr(MSR_IA32_MISC_ENABLE, l, h);
		if (!(l & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP)) {
			printk(KERN_INFO PFX
				"couldn't enable Enhanced SpeedStep\n");
			return -ENODEV;
		}
	}

	freq = get_cur_freq(policy->cpu);
	policy->cpuinfo.transition_latency = 10000;
						/* 10uS transition latency */
	policy->cur = freq;

	pr_debug("centrino_cpu_init: cur=%dkHz\n", policy->cur);

	ret = cpufreq_frequency_table_cpuinfo(policy,
		per_cpu(centrino_model, policy->cpu)->op_points);
	if (ret)
		return (ret);

	cpufreq_frequency_table_get_attr(
		per_cpu(centrino_model, policy->cpu)->op_points, policy->cpu);

	return 0;
}

static int centrino_cpu_exit(struct cpufreq_policy *policy)
{
	unsigned int cpu = policy->cpu;

	if (!per_cpu(centrino_model, cpu))
		return -ENODEV;

	cpufreq_frequency_table_put_attr(cpu);

	per_cpu(centrino_model, cpu) = NULL;

	return 0;
}

/**
 * centrino_verify - verifies a new CPUFreq policy
 * @policy: new policy
 *
 * Limit must be within this model's frequency range at least one
 * border included.
 */
static int centrino_verify (struct cpufreq_policy *policy)
{
	return cpufreq_frequency_table_verify(policy,
			per_cpu(centrino_model, policy->cpu)->op_points);
}

/**
 * centrino_setpolicy - set a new CPUFreq policy
 * @policy: new policy
 * @target_freq: the target frequency
 * @relation: how that frequency relates to achieved frequency
 *	(CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
 *
 * Sets a new CPUFreq policy.
 */
static int centrino_target (struct cpufreq_policy *policy,
			    unsigned int target_freq,
			    unsigned int relation)
{
	unsigned int    newstate = 0;
	unsigned int	msr, oldmsr = 0, h = 0, cpu = policy->cpu;
	struct cpufreq_freqs	freqs;
	int			retval = 0;
	unsigned int		j, k, first_cpu, tmp;
	cpumask_var_t covered_cpus;

	if (unlikely(!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL)))
		return -ENOMEM;

	if (unlikely(per_cpu(centrino_model, cpu) == NULL)) {
		retval = -ENODEV;
		goto out;
	}

	if (unlikely(cpufreq_frequency_table_target(policy,
			per_cpu(centrino_model, cpu)->op_points,
			target_freq,
			relation,
			&newstate))) {
		retval = -EINVAL;
		goto out;
	}

	first_cpu = 1;
	for_each_cpu(j, policy->cpus) {
		int good_cpu;

		/* cpufreq holds the hotplug lock, so we are safe here */
		if (!cpu_online(j))
			continue;

		/*
		 * Support for SMP systems.
		 * Make sure we are running on CPU that wants to change freq
		 */
		if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
			good_cpu = cpumask_any_and(policy->cpus,
						   cpu_online_mask);
		else
			good_cpu = j;

		if (good_cpu >= nr_cpu_ids) {
			pr_debug("couldn't limit to CPUs in this domain\n");
			retval = -EAGAIN;
			if (first_cpu) {
				/* We haven't started the transition yet. */
				goto out;
			}
			break;
		}

		msr = per_cpu(centrino_model, cpu)->op_points[newstate].index;

		if (first_cpu) {
			rdmsr_on_cpu(good_cpu, MSR_IA32_PERF_CTL, &oldmsr, &h);
			if (msr == (oldmsr & 0xffff)) {
				pr_debug("no change needed - msr was and needs "
					"to be %x\n", oldmsr);
				retval = 0;
				goto out;
			}

			freqs.old = extract_clock(oldmsr, cpu, 0);
			freqs.new = extract_clock(msr, cpu, 0);

			pr_debug("target=%dkHz old=%d new=%d msr=%04x\n",
				target_freq, freqs.old, freqs.new, msr);

			for_each_cpu(k, policy->cpus) {
				if (!cpu_online(k))
					continue;
				freqs.cpu = k;
				cpufreq_notify_transition(&freqs,
					CPUFREQ_PRECHANGE);
			}

			first_cpu = 0;
			/* all but 16 LSB are reserved, treat them with care */
			oldmsr &= ~0xffff;
			msr &= 0xffff;
			oldmsr |= msr;
		}

		wrmsr_on_cpu(good_cpu, MSR_IA32_PERF_CTL, oldmsr, h);
		if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
			break;

		cpumask_set_cpu(j, covered_cpus);
	}

	for_each_cpu(k, policy->cpus) {
		if (!cpu_online(k))
			continue;
		freqs.cpu = k;
		cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
	}

	if (unlikely(retval)) {
		/*
		 * We have failed halfway through the frequency change.
		 * We have sent callbacks to policy->cpus and
		 * MSRs have already been written on coverd_cpus.
		 * Best effort undo..
		 */

		for_each_cpu(j, covered_cpus)
			wrmsr_on_cpu(j, MSR_IA32_PERF_CTL, oldmsr, h);

		tmp = freqs.new;
		freqs.new = freqs.old;
		freqs.old = tmp;
		for_each_cpu(j, policy->cpus) {
			if (!cpu_online(j))
				continue;
			cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
			cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
		}
	}
	retval = 0;

out:
	free_cpumask_var(covered_cpus);
	return retval;
}

static struct freq_attr* centrino_attr[] = {
	&cpufreq_freq_attr_scaling_available_freqs,
	NULL,
};

static struct cpufreq_driver centrino_driver = {
	.name		= "centrino", /* should be speedstep-centrino,
					 but there's a 16 char limit */
	.init		= centrino_cpu_init,
	.exit		= centrino_cpu_exit,
	.verify		= centrino_verify,
	.target		= centrino_target,
	.get		= get_cur_freq,
	.attr           = centrino_attr,
	.owner		= THIS_MODULE,
};


/**
 * centrino_init - initializes the Enhanced SpeedStep CPUFreq driver
 *
 * Initializes the Enhanced SpeedStep support. Returns -ENODEV on
 * unsupported devices, -ENOENT if there's no voltage table for this
 * particular CPU model, -EINVAL on problems during initiatization,
 * and zero on success.
 *
 * This is quite picky.  Not only does the CPU have to advertise the
 * "est" flag in the cpuid capability flags, we look for a specific
 * CPU model and stepping, and we need to have the exact model name in
 * our voltage tables.  That is, be paranoid about not releasing
 * someone's valuable magic smoke.
 */
static int __init centrino_init(void)
{
	struct cpuinfo_x86 *cpu = &cpu_data(0);

	if (!cpu_has(cpu, X86_FEATURE_EST))
		return -ENODEV;

	return cpufreq_register_driver(&centrino_driver);
}

static void __exit centrino_exit(void)
{
	cpufreq_unregister_driver(&centrino_driver);
}

MODULE_AUTHOR ("Jeremy Fitzhardinge <jeremy@goop.org>");
MODULE_DESCRIPTION ("Enhanced SpeedStep driver for Intel Pentium M processors.");
MODULE_LICENSE ("GPL");

late_initcall(centrino_init);
module_exit(centrino_exit);
.fromsec = { ALL_EXIT_SECTIONS, NULL }, .tosec = { ALL_INIT_SECTIONS, NULL }, .mismatch = EXIT_TO_INIT, }, /* Do not export init/exit functions or data */ { .fromsec = { "__ksymtab*", NULL }, .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL }, .mismatch = EXPORT_TO_INIT_EXIT } }; static int section_mismatch(const char *fromsec, const char *tosec) { int i; int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck); const struct sectioncheck *check = &sectioncheck[0]; for (i = 0; i < elems; i++) { if (match(fromsec, check->fromsec) && match(tosec, check->tosec)) return check->mismatch; check++; } return NO_MISMATCH; } /** * Whitelist to allow certain references to pass with no warning. * * Pattern 1: * If a module parameter is declared __initdata and permissions=0 * then this is legal despite the warning generated. * We cannot see value of permissions here, so just ignore * this pattern. * The pattern is identified by: * tosec = .init.data * fromsec = .data* * atsym =__param* * * Pattern 2: * Many drivers utilise a *driver container with references to * add, remove, probe functions etc. * These functions may often be marked __init and we do not want to * warn here. * the pattern is identified by: * tosec = init or exit section * fromsec = data section * atsym = *driver, *_template, *_sht, *_ops, *_probe, * *probe_one, *_console, *_timer * * Pattern 3: * Whitelist all references from .head.text to any init section * * Pattern 4: * Some symbols belong to init section but still it is ok to reference * these from non-init sections as these symbols don't have any memory * allocated for them and symbol address and value are same. So even * if init section is freed, its ok to reference those symbols. * For ex. symbols marking the init section boundaries. * This pattern is identified by * refsymname = __init_begin, _sinittext, _einittext * **/ static int secref_whitelist(const char *fromsec, const char *fromsym, const char *tosec, const char *tosym) { /* Check for pattern 1 */ if (match(tosec, init_data_sections) && match(fromsec, data_sections) && (strncmp(fromsym, "__param", strlen("__param")) == 0)) return 0; /* Check for pattern 2 */ if (match(tosec, init_exit_sections) && match(fromsec, data_sections) && match(fromsym, symbol_white_list)) return 0; /* Check for pattern 3 */ if (match(fromsec, head_sections) && match(tosec, init_sections)) return 0; /* Check for pattern 4 */ if (match(tosym, linker_symbols)) return 0; return 1; } /** * Find symbol based on relocation record info. * In some cases the symbol supplied is a valid symbol so * return refsym. If st_name != 0 we assume this is a valid symbol. * In other cases the symbol needs to be looked up in the symbol table * based on section and address. * **/ static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr, Elf_Sym *relsym) { Elf_Sym *sym; Elf_Sym *near = NULL; Elf64_Sword distance = 20; Elf64_Sword d; if (relsym->st_name != 0) return relsym; for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { if (sym->st_shndx != relsym->st_shndx) continue; if (ELF_ST_TYPE(sym->st_info) == STT_SECTION) continue; if (sym->st_value == addr) return sym; /* Find a symbol nearby - addr are maybe negative */ d = sym->st_value - addr; if (d < 0) d = addr - sym->st_value; if (d < distance) { distance = d; near = sym; } } /* We need a close match */ if (distance < 20) return near; else return NULL; } static inline int is_arm_mapping_symbol(const char *str) { return str[0] == '$' && strchr("atd", str[1]) && (str[2] == '\0' || str[2] == '.'); } /* * If there's no name there, ignore it; likewise, ignore it if it's * one of the magic symbols emitted used by current ARM tools. * * Otherwise if find_symbols_between() returns those symbols, they'll * fail the whitelist tests and cause lots of false alarms ... fixable * only by merging __exit and __init sections into __text, bloating * the kernel (which is especially evil on embedded platforms). */ static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym) { const char *name = elf->strtab + sym->st_name; if (!name || !strlen(name)) return 0; return !is_arm_mapping_symbol(name); } /* * Find symbols before or equal addr and after addr - in the section sec. * If we find two symbols with equal offset prefer one with a valid name. * The ELF format may have a better way to detect what type of symbol * it is, but this works for now. **/ static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr, const char *sec) { Elf_Sym *sym; Elf_Sym *near = NULL; Elf_Addr distance = ~0; for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { const char *symsec; if (sym->st_shndx >= SHN_LORESERVE) continue; symsec = sec_name(elf, sym->st_shndx); if (strcmp(symsec, sec) != 0) continue; if (!is_valid_name(elf, sym)) continue; if (sym->st_value <= addr) { if ((addr - sym->st_value) < distance) { distance = addr - sym->st_value; near = sym; } else if ((addr - sym->st_value) == distance) { near = sym; } } } return near; } /* * Convert a section name to the function/data attribute * .init.text => __init * .cpuinit.data => __cpudata * .memexitconst => __memconst * etc. */ static char *sec2annotation(const char *s) { if (match(s, init_exit_sections)) { char *p = malloc(20); char *r = p; *p++ = '_'; *p++ = '_'; if (*s == '.') s++; while (*s && *s != '.') *p++ = *s++; *p = '\0'; if (*s == '.') s++; if (strstr(s, "rodata") != NULL) strcat(p, "const "); else if (strstr(s, "data") != NULL) strcat(p, "data "); else strcat(p, " "); return r; /* we leak her but we do not care */ } else { return ""; } } static int is_function(Elf_Sym *sym) { if (sym) return ELF_ST_TYPE(sym->st_info) == STT_FUNC; else return -1; } /* * Print a warning about a section mismatch. * Try to find symbols near it so user can find it. * Check whitelist before warning - it may be a false positive. */ static void report_sec_mismatch(const char *modname, enum mismatch mismatch, const char *fromsec, unsigned long long fromaddr, const char *fromsym, int from_is_func, const char *tosec, const char *tosym, int to_is_func) { const char *from, *from_p; const char *to, *to_p; switch (from_is_func) { case 0: from = "variable"; from_p = ""; break; case 1: from = "function"; from_p = "()"; break; default: from = "(unknown reference)"; from_p = ""; break; } switch (to_is_func) { case 0: to = "variable"; to_p = ""; break; case 1: to = "function"; to_p = "()"; break; default: to = "(unknown reference)"; to_p = ""; break; } sec_mismatch_count++; if (!sec_mismatch_verbose) return; warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s " "to the %s %s:%s%s\n", modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec, tosym, to_p); switch (mismatch) { case TEXT_TO_INIT: fprintf(stderr, "The function %s%s() references\n" "the %s %s%s%s.\n" "This is often because %s lacks a %s\n" "annotation or the annotation of %s is wrong.\n", sec2annotation(fromsec), fromsym, to, sec2annotation(tosec), tosym, to_p, fromsym, sec2annotation(tosec), tosym); break; case DATA_TO_INIT: { const char **s = symbol_white_list; fprintf(stderr, "The variable %s references\n" "the %s %s%s%s\n" "If the reference is valid then annotate the\n" "variable with __init* or __refdata (see linux/init.h) " "or name the variable:\n", fromsym, to, sec2annotation(tosec), tosym, to_p); while (*s) fprintf(stderr, "%s, ", *s++); fprintf(stderr, "\n"); break; } case TEXT_TO_EXIT: fprintf(stderr, "The function %s() references a %s in an exit section.\n" "Often the %s %s%s has valid usage outside the exit section\n" "and the fix is to remove the %sannotation of %s.\n", fromsym, to, to, tosym, to_p, sec2annotation(tosec), tosym); break; case DATA_TO_EXIT: { const char **s = symbol_white_list; fprintf(stderr, "The variable %s references\n" "the %s %s%s%s\n" "If the reference is valid then annotate the\n" "variable with __exit* (see linux/init.h) or " "name the variable:\n", fromsym, to, sec2annotation(tosec), tosym, to_p); while (*s) fprintf(stderr, "%s, ", *s++); fprintf(stderr, "\n"); break; } case XXXINIT_TO_INIT: case XXXEXIT_TO_EXIT: fprintf(stderr, "The %s %s%s%s references\n" "a %s %s%s%s.\n" "If %s is only used by %s then\n" "annotate %s with a matching annotation.\n", from, sec2annotation(fromsec), fromsym, from_p, to, sec2annotation(tosec), tosym, to_p, tosym, fromsym, tosym); break; case INIT_TO_EXIT: fprintf(stderr, "The %s %s%s%s references\n" "a %s %s%s%s.\n" "This is often seen when error handling " "in the init function\n" "uses functionality in the exit path.\n" "The fix is often to remove the %sannotation of\n" "%s%s so it may be used outside an exit section.\n", from, sec2annotation(fromsec), fromsym, from_p, to, sec2annotation(tosec), tosym, to_p, sec2annotation(tosec), tosym, to_p); break; case EXIT_TO_INIT: fprintf(stderr, "The %s %s%s%s references\n" "a %s %s%s%s.\n" "This is often seen when error handling " "in the exit function\n" "uses functionality in the init path.\n" "The fix is often to remove the %sannotation of\n" "%s%s so it may be used outside an init section.\n", from, sec2annotation(fromsec), fromsym, from_p, to, sec2annotation(tosec), tosym, to_p, sec2annotation(tosec), tosym, to_p); break; case EXPORT_TO_INIT_EXIT: fprintf(stderr, "The symbol %s is exported and annotated %s\n" "Fix this by removing the %sannotation of %s " "or drop the export.\n", tosym, sec2annotation(tosec), sec2annotation(tosec), tosym); case NO_MISMATCH: /* To get warnings on missing members */ break; } fprintf(stderr, "\n"); } static void check_section_mismatch(const char *modname, struct elf_info *elf, Elf_Rela *r, Elf_Sym *sym, const char *fromsec) { const char *tosec; enum mismatch mismatch; tosec = sec_name(elf, sym->st_shndx); mismatch = section_mismatch(fromsec, tosec); if (mismatch != NO_MISMATCH) { Elf_Sym *to; Elf_Sym *from; const char *tosym; const char *fromsym; from = find_elf_symbol2(elf, r->r_offset, fromsec); fromsym = sym_name(elf, from); to = find_elf_symbol(elf, r->r_addend, sym); tosym = sym_name(elf, to); /* check whitelist - we may ignore it */ if (secref_whitelist(fromsec, fromsym, tosec, tosym)) { report_sec_mismatch(modname, mismatch, fromsec, r->r_offset, fromsym, is_function(from), tosec, tosym, is_function(to)); } } } static unsigned int *reloc_location(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) { Elf_Shdr *sechdrs = elf->sechdrs; int section = sechdr->sh_info; return (void *)elf->hdr + sechdrs[section].sh_offset + (r->r_offset - sechdrs[section].sh_addr); } static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) { unsigned int r_typ = ELF_R_TYPE(r->r_info); unsigned int *location = reloc_location(elf, sechdr, r); switch (r_typ) { case R_386_32: r->r_addend = TO_NATIVE(*location); break; case R_386_PC32: r->r_addend = TO_NATIVE(*location) + 4; /* For CONFIG_RELOCATABLE=y */ if (elf->hdr->e_type == ET_EXEC) r->r_addend += r->r_offset; break; } return 0; } static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) { unsigned int r_typ = ELF_R_TYPE(r->r_info); switch (r_typ) { case R_ARM_ABS32: /* From ARM ABI: (S + A) | T */ r->r_addend = (int)(long) (elf->symtab_start + ELF_R_SYM(r->r_info)); break; case R_ARM_PC24: /* From ARM ABI: ((S + A) | T) - P */ r->r_addend = (int)(long)(elf->hdr + sechdr->sh_offset + (r->r_offset - sechdr->sh_addr)); break; default: return 1; } return 0; } static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) { unsigned int r_typ = ELF_R_TYPE(r->r_info); unsigned int *location = reloc_location(elf, sechdr, r); unsigned int inst; if (r_typ == R_MIPS_HI16) return 1; /* skip this */ inst = TO_NATIVE(*location); switch (r_typ) { case R_MIPS_LO16: r->r_addend = inst & 0xffff; break; case R_MIPS_26: r->r_addend = (inst & 0x03ffffff) << 2; break; case R_MIPS_32: r->r_addend = inst; break; } return 0; } static void section_rela(const char *modname, struct elf_info *elf, Elf_Shdr *sechdr) { Elf_Sym *sym; Elf_Rela *rela; Elf_Rela r; unsigned int r_sym; const char *fromsec; Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset; Elf_Rela *stop = (void *)start + sechdr->sh_size; fromsec = sech_name(elf, sechdr); fromsec += strlen(".rela"); /* if from section (name) is know good then skip it */ if (match(fromsec, section_white_list)) return; for (rela = start; rela < stop; rela++) { r.r_offset = TO_NATIVE(rela->r_offset); #if KERNEL_ELFCLASS == ELFCLASS64 if (elf->hdr->e_machine == EM_MIPS) { unsigned int r_typ; r_sym = ELF64_MIPS_R_SYM(rela->r_info); r_sym = TO_NATIVE(r_sym); r_typ = ELF64_MIPS_R_TYPE(rela->r_info); r.r_info = ELF64_R_INFO(r_sym, r_typ); } else { r.r_info = TO_NATIVE(rela->r_info); r_sym = ELF_R_SYM(r.r_info); } #else r.r_info = TO_NATIVE(rela->r_info); r_sym = ELF_R_SYM(r.r_info); #endif r.r_addend = TO_NATIVE(rela->r_addend); sym = elf->symtab_start + r_sym; /* Skip special sections */ if (sym->st_shndx >= SHN_LORESERVE) continue; check_section_mismatch(modname, elf, &r, sym, fromsec); } } static void section_rel(const char *modname, struct elf_info *elf, Elf_Shdr *sechdr) { Elf_Sym *sym; Elf_Rel *rel; Elf_Rela r; unsigned int r_sym; const char *fromsec; Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset; Elf_Rel *stop = (void *)start + sechdr->sh_size; fromsec = sech_name(elf, sechdr); fromsec += strlen(".rel"); /* if from section (name) is know good then skip it */ if (match(fromsec, section_white_list)) return; for (rel = start; rel < stop; rel++) { r.r_offset = TO_NATIVE(rel->r_offset); #if KERNEL_ELFCLASS == ELFCLASS64 if (elf->hdr->e_machine == EM_MIPS) { unsigned int r_typ; r_sym = ELF64_MIPS_R_SYM(rel->r_info); r_sym = TO_NATIVE(r_sym); r_typ = ELF64_MIPS_R_TYPE(rel->r_info); r.r_info = ELF64_R_INFO(r_sym, r_typ); } else { r.r_info = TO_NATIVE(rel->r_info); r_sym = ELF_R_SYM(r.r_info); } #else r.r_info = TO_NATIVE(rel->r_info); r_sym = ELF_R_SYM(r.r_info); #endif r.r_addend = 0; switch (elf->hdr->e_machine) { case EM_386: if (addend_386_rel(elf, sechdr, &r)) continue; break; case EM_ARM: if (addend_arm_rel(elf, sechdr, &r)) continue; break; case EM_MIPS: if (addend_mips_rel(elf, sechdr, &r)) continue; break; } sym = elf->symtab_start + r_sym; /* Skip special sections */ if (sym->st_shndx >= SHN_LORESERVE) continue; check_section_mismatch(modname, elf, &r, sym, fromsec); } } /** * A module includes a number of sections that are discarded * either when loaded or when used as built-in. * For loaded modules all functions marked __init and all data * marked __initdata will be discarded when the module has been intialized. * Likewise for modules used built-in the sections marked __exit * are discarded because __exit marked function are supposed to be called * only when a module is unloaded which never happens for built-in modules. * The check_sec_ref() function traverses all relocation records * to find all references to a section that reference a section that will * be discarded and warns about it. **/ static void check_sec_ref(struct module *mod, const char *modname, struct elf_info *elf) { int i; Elf_Shdr *sechdrs = elf->sechdrs; /* Walk through all sections */ for (i = 0; i < elf->hdr->e_shnum; i++) { check_section(modname, elf, &elf->sechdrs[i]); /* We want to process only relocation sections and not .init */ if (sechdrs[i].sh_type == SHT_RELA) section_rela(modname, elf, &elf->sechdrs[i]); else if (sechdrs[i].sh_type == SHT_REL) section_rel(modname, elf, &elf->sechdrs[i]); } } static void get_markers(struct elf_info *info, struct module *mod) { const Elf_Shdr *sh = &info->sechdrs[info->markers_strings_sec]; const char *strings = (const char *) info->hdr + sh->sh_offset; const Elf_Sym *sym, *first_sym, *last_sym; size_t n; if (!info->markers_strings_sec) return; /* * First count the strings. We look for all the symbols defined * in the __markers_strings section named __mstrtab_*. For * these local names, the compiler puts a random .NNN suffix on, * so the names don't correspond exactly. */ first_sym = last_sym = NULL; n = 0; for (sym = info->symtab_start; sym < info->symtab_stop; sym++) if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT && sym->st_shndx == info->markers_strings_sec && !strncmp(info->strtab + sym->st_name, "__mstrtab_", sizeof "__mstrtab_" - 1)) { if (first_sym == NULL) first_sym = sym; last_sym = sym; ++n; } if (n == 0) return; /* * Now collect each name and format into a line for the output. * Lines look like: * marker_name vmlinux marker %s format %d * The format string after the second \t can use whitespace. */ mod->markers = NOFAIL(malloc(sizeof mod->markers[0] * n)); mod->nmarkers = n; n = 0; for (sym = first_sym; sym <= last_sym; sym++) if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT && sym->st_shndx == info->markers_strings_sec && !strncmp(info->strtab + sym->st_name, "__mstrtab_", sizeof "__mstrtab_" - 1)) { const char *name = strings + sym->st_value; const char *fmt = strchr(name, '\0') + 1; char *line = NULL; asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt); NOFAIL(line); mod->markers[n++] = line; } } static void read_symbols(char *modname) { const char *symname; char *version; char *license; struct module *mod; struct elf_info info = { }; Elf_Sym *sym; if (!parse_elf(&info, modname)) return; mod = new_module(modname); /* When there's no vmlinux, don't print warnings about * unresolved symbols (since there'll be too many ;) */ if (is_vmlinux(modname)) { have_vmlinux = 1; mod->skip = 1; } license = get_modinfo(info.modinfo, info.modinfo_len, "license"); if (info.modinfo && !license && !is_vmlinux(modname)) warn("modpost: missing MODULE_LICENSE() in %s\n" "see include/linux/module.h for " "more information\n", modname); while (license) { if (license_is_gpl_compatible(license)) mod->gpl_compatible = 1; else { mod->gpl_compatible = 0; break; } license = get_next_modinfo(info.modinfo, info.modinfo_len, "license", license); } for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { symname = info.strtab + sym->st_name; handle_modversions(mod, &info, sym, symname); handle_moddevtable(mod, &info, sym, symname); } if (!is_vmlinux(modname) || (is_vmlinux(modname) && vmlinux_section_warnings)) check_sec_ref(mod, modname, &info); version = get_modinfo(info.modinfo, info.modinfo_len, "version"); if (version) maybe_frob_rcs_version(modname, version, info.modinfo, version - (char *)info.hdr); if (version || (all_versions && !is_vmlinux(modname))) get_src_version(modname, mod->srcversion, sizeof(mod->srcversion)-1); get_markers(&info, mod); parse_elf_finish(&info); /* Our trick to get versioning for module struct etc. - it's * never passed as an argument to an exported function, so * the automatic versioning doesn't pick it up, but it's really * important anyhow */ if (modversions) mod->unres = alloc_symbol("module_layout", 0, mod->unres); } #define SZ 500 /* We first write the generated file into memory using the * following helper, then compare to the file on disk and * only update the later if anything changed */ void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf, const char *fmt, ...) { char tmp[SZ]; int len; va_list ap; va_start(ap, fmt); len = vsnprintf(tmp, SZ, fmt, ap); buf_write(buf, tmp, len); va_end(ap); } void buf_write(struct buffer *buf, const char *s, int len) { if (buf->size - buf->pos < len) { buf->size += len + SZ; buf->p = realloc(buf->p, buf->size); } strncpy(buf->p + buf->pos, s, len); buf->pos += len; } static void check_for_gpl_usage(enum export exp, const char *m, const char *s) { const char *e = is_vmlinux(m) ?"":".ko"; switch (exp) { case export_gpl: fatal("modpost: GPL-incompatible module %s%s " "uses GPL-only symbol '%s'\n", m, e, s); break; case export_unused_gpl: fatal("modpost: GPL-incompatible module %s%s " "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s); break; case export_gpl_future: warn("modpost: GPL-incompatible module %s%s " "uses future GPL-only symbol '%s'\n", m, e, s); break; case export_plain: case export_unused: case export_unknown: /* ignore */ break; } } static void check_for_unused(enum export exp, const char *m, const char *s) { const char *e = is_vmlinux(m) ?"":".ko"; switch (exp) { case export_unused: case export_unused_gpl: warn("modpost: module %s%s " "uses symbol '%s' marked UNUSED\n", m, e, s); break; default: /* ignore */ break; } } static void check_exports(struct module *mod) { struct symbol *s, *exp; for (s = mod->unres; s; s = s->next) { const char *basename; exp = find_symbol(s->name); if (!exp || exp->module == mod) continue; basename = strrchr(mod->name, '/'); if (basename) basename++; else basename = mod->name; if (!mod->gpl_compatible) check_for_gpl_usage(exp->export, basename, exp->name); check_for_unused(exp->export, basename, exp->name); } } /** * Header for the generated file **/ static void add_header(struct buffer *b, struct module *mod) { buf_printf(b, "#include <linux/module.h>\n"); buf_printf(b, "#include <linux/vermagic.h>\n"); buf_printf(b, "#include <linux/compiler.h>\n"); buf_printf(b, "\n"); buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n"); buf_printf(b, "\n"); buf_printf(b, "struct module __this_module\n"); buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n"); buf_printf(b, " .name = KBUILD_MODNAME,\n"); if (mod->has_init) buf_printf(b, " .init = init_module,\n"); if (mod->has_cleanup) buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n" " .exit = cleanup_module,\n" "#endif\n"); buf_printf(b, " .arch = MODULE_ARCH_INIT,\n"); buf_printf(b, "};\n"); } void add_staging_flag(struct buffer *b, const char *name) { static const char *staging_dir = "drivers/staging"; if (strncmp(staging_dir, name, strlen(staging_dir)) == 0) buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n"); } /** * Record CRCs for unresolved symbols **/ static int add_versions(struct buffer *b, struct module *mod) { struct symbol *s, *exp; int err = 0; for (s = mod->unres; s; s = s->next) { exp = find_symbol(s->name); if (!exp || exp->module == mod) { if (have_vmlinux && !s->weak) { if (warn_unresolved) { warn("\"%s\" [%s.ko] undefined!\n", s->name, mod->name); } else { merror("\"%s\" [%s.ko] undefined!\n", s->name, mod->name); err = 1; } } continue; } s->module = exp->module; s->crc_valid = exp->crc_valid; s->crc = exp->crc; } if (!modversions) return err; buf_printf(b, "\n"); buf_printf(b, "static const struct modversion_info ____versions[]\n"); buf_printf(b, "__used\n"); buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n"); for (s = mod->unres; s; s = s->next) { if (!s->module) continue; if (!s->crc_valid) { warn("\"%s\" [%s.ko] has no CRC!\n", s->name, mod->name); continue; } buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name); } buf_printf(b, "};\n"); return err; } static void add_depends(struct buffer *b, struct module *mod, struct module *modules) { struct symbol *s; struct module *m; int first = 1; for (m = modules; m; m = m->next) m->seen = is_vmlinux(m->name); buf_printf(b, "\n"); buf_printf(b, "static const char __module_depends[]\n"); buf_printf(b, "__used\n"); buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n"); buf_printf(b, "\"depends="); for (s = mod->unres; s; s = s->next) { const char *p; if (!s->module) continue; if (s->module->seen) continue; s->module->seen = 1; p = strrchr(s->module->name, '/'); if (p) p++; else p = s->module->name; buf_printf(b, "%s%s", first ? "" : ",", p); first = 0; } buf_printf(b, "\";\n"); } static void add_srcversion(struct buffer *b, struct module *mod) { if (mod->srcversion[0]) { buf_printf(b, "\n"); buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n", mod->srcversion); } } static void write_if_changed(struct buffer *b, const char *fname) { char *tmp; FILE *file; struct stat st; file = fopen(fname, "r"); if (!file) goto write; if (fstat(fileno(file), &st) < 0) goto close_write; if (st.st_size != b->pos) goto close_write; tmp = NOFAIL(malloc(b->pos)); if (fread(tmp, 1, b->pos, file) != b->pos) goto free_write; if (memcmp(tmp, b->p, b->pos) != 0) goto free_write; free(tmp); fclose(file); return; free_write: free(tmp); close_write: fclose(file); write: file = fopen(fname, "w"); if (!file) { perror(fname); exit(1); } if (fwrite(b->p, 1, b->pos, file) != b->pos) { perror(fname); exit(1); } fclose(file); } /* parse Module.symvers file. line format: * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something] **/ static void read_dump(const char *fname, unsigned int kernel) { unsigned long size, pos = 0; void *file = grab_file(fname, &size); char *line; if (!file) /* No symbol versions, silently ignore */ return; while ((line = get_next_line(&pos, file, size))) { char *symname, *modname, *d, *export, *end; unsigned int crc; struct module *mod; struct symbol *s; if (!(symname = strchr(line, '\t'))) goto fail; *symname++ = '\0'; if (!(modname = strchr(symname, '\t'))) goto fail; *modname++ = '\0'; if ((export = strchr(modname, '\t')) != NULL) *export++ = '\0'; if (export && ((end = strchr(export, '\t')) != NULL)) *end = '\0'; crc = strtoul(line, &d, 16); if (*symname == '\0' || *modname == '\0' || *d != '\0') goto fail; mod = find_module(modname); if (!mod) { if (is_vmlinux(modname)) have_vmlinux = 1; mod = new_module(modname); mod->skip = 1; } s = sym_add_exported(symname, mod, export_no(export)); s->kernel = kernel; s->preloaded = 1; sym_update_crc(symname, mod, crc, export_no(export)); } return; fail: fatal("parse error in symbol dump file\n"); } /* For normal builds always dump all symbols. * For external modules only dump symbols * that are not read from kernel Module.symvers. **/ static int dump_sym(struct symbol *sym) { if (!external_module) return 1; if (sym->vmlinux || sym->kernel) return 0; return 1; } static void write_dump(const char *fname) { struct buffer buf = { }; struct symbol *symbol; int n; for (n = 0; n < SYMBOL_HASH_SIZE ; n++) { symbol = symbolhash[n]; while (symbol) { if (dump_sym(symbol)) buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n", symbol->crc, symbol->name, symbol->module->name, export_str(symbol->export)); symbol = symbol->next; } } write_if_changed(&buf, fname); } static void add_marker(struct module *mod, const char *name, const char *fmt) { char *line = NULL; asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt); NOFAIL(line); mod->markers = NOFAIL(realloc(mod->markers, ((mod->nmarkers + 1) * sizeof mod->markers[0]))); mod->markers[mod->nmarkers++] = line; } static void read_markers(const char *fname) { unsigned long size, pos = 0; void *file = grab_file(fname, &size); char *line; if (!file) /* No old markers, silently ignore */ return; while ((line = get_next_line(&pos, file, size))) { char *marker, *modname, *fmt; struct module *mod; marker = line; modname = strchr(marker, '\t'); if (!modname) goto fail; *modname++ = '\0'; fmt = strchr(modname, '\t'); if (!fmt) goto fail; *fmt++ = '\0'; if (*marker == '\0' || *modname == '\0') goto fail; mod = find_module(modname); if (!mod) { mod = new_module(modname); mod->skip = 1; } if (is_vmlinux(modname)) { have_vmlinux = 1; mod->skip = 0; } if (!mod->skip) add_marker(mod, marker, fmt); } release_file(file, size); return; fail: fatal("parse error in markers list file\n"); } static int compare_strings(const void *a, const void *b) { return strcmp(*(const char **) a, *(const char **) b); } static void write_markers(const char *fname) { struct buffer buf = { }; struct module *mod; size_t i; for (mod = modules; mod; mod = mod->next) if ((!external_module || !mod->skip) && mod->markers != NULL) { /* * Sort the strings so we can skip duplicates when * we write them out. */ qsort(mod->markers, mod->nmarkers, sizeof mod->markers[0], &compare_strings); for (i = 0; i < mod->nmarkers; ++i) { char *line = mod->markers[i]; buf_write(&buf, line, strlen(line)); while (i + 1 < mod->nmarkers && !strcmp(mod->markers[i], mod->markers[i + 1])) free(mod->markers[i++]); free(mod->markers[i]); } free(mod->markers); mod->markers = NULL; } write_if_changed(&buf, fname); } struct ext_sym_list { struct ext_sym_list *next; const char *file; }; int main(int argc, char **argv) { struct module *mod; struct buffer buf = { }; char *kernel_read = NULL, *module_read = NULL; char *dump_write = NULL; char *markers_read = NULL; char *markers_write = NULL; int opt; int err; struct ext_sym_list *extsym_iter; struct ext_sym_list *extsym_start = NULL; while ((opt = getopt(argc, argv, "i:I:e:cmsSo:awM:K:")) != -1) { switch (opt) { case 'i': kernel_read = optarg; break; case 'I': module_read = optarg; external_module = 1; break; case 'c': cross_build = 1; break; case 'e': external_module = 1; extsym_iter = NOFAIL(malloc(sizeof(*extsym_iter))); extsym_iter->next = extsym_start; extsym_iter->file = optarg; extsym_start = extsym_iter; break; case 'm': modversions = 1; break; case 'o': dump_write = optarg; break; case 'a': all_versions = 1; break; case 's': vmlinux_section_warnings = 0; break; case 'S': sec_mismatch_verbose = 0; break; case 'w': warn_unresolved = 1; break; case 'M': markers_write = optarg; break; case 'K': markers_read = optarg; break; default: exit(1); } } if (kernel_read) read_dump(kernel_read, 1); if (module_read) read_dump(module_read, 0); while (extsym_start) { read_dump(extsym_start->file, 0); extsym_iter = extsym_start->next; free(extsym_start); extsym_start = extsym_iter; } while (optind < argc) read_symbols(argv[optind++]); for (mod = modules; mod; mod = mod->next) { if (mod->skip) continue; check_exports(mod); } err = 0; for (mod = modules; mod; mod = mod->next) { char fname[strlen(mod->name) + 10]; if (mod->skip) continue; buf.pos = 0; add_header(&buf, mod); add_staging_flag(&buf, mod->name); err |= add_versions(&buf, mod); add_depends(&buf, mod, modules); add_moddevtable(&buf, mod); add_srcversion(&buf, mod); sprintf(fname, "%s.mod.c", mod->name); write_if_changed(&buf, fname); } if (dump_write) write_dump(dump_write); if (sec_mismatch_count && !sec_mismatch_verbose) warn("modpost: Found %d section mismatch(es).\n" "To see full details build your kernel with:\n" "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n", sec_mismatch_count); if (markers_read) read_markers(markers_read); if (markers_write) write_markers(markers_write); return err; }